Executing javascript inside div - Stack Overflow

Question updated. Update at the bottom-I have to put script on a page. Unfortunately, I don't have

Question updated. Update at the bottom-

I have to put script on a page. Unfortunately, I don't have control over the header or footer or even the body tag. All, I have is a set of inner html that goes inside a wrapping DIV which sits inside the body tag. Something like this:

<body>
     <div id="wrap">
         <!-- My controlled content -->
     </div>
</body>

Now, I have to place some javascript. So, I obviously put it inside the section that I control. The problem is that the javascript is not being executed. I even put an alert but there is NO alert box popping up. The final code looks something like this:

<body>
     <div id="wrap">
         <div id="mycontent">
              <script type="text/javascript" language="javascript" src="somesource"></script>

              <script>
                  var x, y; 
                  //Do something here with x,y;
                  alert("hello");
              </script>
         </div>
     </div>
</body>

Any idea what could be wrong? And how I can fix it?

Thanks.

UPDATE: I am not sure if this makes any difference, but I forgot to mention that all this HTML is present inside a frame tag (under a frameset). Will that make a difference?

Question updated. Update at the bottom-

I have to put script on a page. Unfortunately, I don't have control over the header or footer or even the body tag. All, I have is a set of inner html that goes inside a wrapping DIV which sits inside the body tag. Something like this:

<body>
     <div id="wrap">
         <!-- My controlled content -->
     </div>
</body>

Now, I have to place some javascript. So, I obviously put it inside the section that I control. The problem is that the javascript is not being executed. I even put an alert but there is NO alert box popping up. The final code looks something like this:

<body>
     <div id="wrap">
         <div id="mycontent">
              <script type="text/javascript" language="javascript" src="somesource"></script>

              <script>
                  var x, y; 
                  //Do something here with x,y;
                  alert("hello");
              </script>
         </div>
     </div>
</body>

Any idea what could be wrong? And how I can fix it?

Thanks.

UPDATE: I am not sure if this makes any difference, but I forgot to mention that all this HTML is present inside a frame tag (under a frameset). Will that make a difference?

Share Improve this question edited Apr 1, 2013 at 17:15 Blueboye asked Apr 1, 2013 at 17:01 BlueboyeBlueboye 1,4944 gold badges27 silver badges54 bronze badges 7
  • Did you mean to say there is NO alert popping up with your code? This line is confusing: "I even put an alert but there is alert box popping up" – Kai Qing Commented Apr 1, 2013 at 17:06
  • Are you making these changes dynamically ? – hop Commented Apr 1, 2013 at 17:07
  • Does your <script> appear in the source code of the resulting page? Do you get any error messages? Is your <script> valid for the page's doctype (i.e. do you need to set a type attribute)? – Paul S. Commented Apr 1, 2013 at 17:07
  • Did you check the console? – nicosantangelo Commented Apr 1, 2013 at 17:08
  • If its possible for you try opening the url in chrome, then open developer tools (Ctrl + Shift + I) and see the network section. If you see your script name in the list then it is getting loaded properly, or an error will be shown if browser fails to load the script – vdua Commented Apr 1, 2013 at 17:08
 |  Show 2 more ments

4 Answers 4

Reset to default 1

EDIT:

I see you are trying to load an external javascript file - are you sure that file exists?


Instead, put some javascript wrapped in the document ready event:

The below is a way to run code when the dom loads using jQuery. However, if you don't want to use jQuery, see the last part of this answer.

$(document).ready(function(){
// dom ready
   alert("hello");
});

You can further control the div my content like so:

$(document).ready(function(){
// dom ready
   alert("hello");
   $("#mycontent").hide(); // for example, hide the div
});

Non jQuery:

At the bottom of your HTML page, place your code in <script> tags - this way the code will run after the elements on the page have loaded.

Disable the XSS filter.

As a security auditor, I just realized that Chrome nor Internet Explorer (IE) were rendering my alert().

Run chrome with:

C:\Program Files (x86)\Google\Chrome\Application>chrome.exe --disable-xss-auditor

Javascript needs an event to be triggered. Like another poster mentioned, document.ready is the document loaded event. Most other events are user triggered like a mouse click etc. You can't just put Javascript halfway down an HTML file and expect it to execute. It needs to be tied to an event.

Just a ment: document.ready: is not javascript, $(document).ready is jQuery. Use DOMContentLoaded instead:

$(document).ready(function(){
// dom ready
   alert("hello");
});

In modern browsers (see the support in the link above) it is equivalent to

window.addEventListener("DOMContentLoaded",function() {
// dom ready
   alert("hello");
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744540487a4579693.html

相关推荐

  • Executing javascript inside div - Stack Overflow

    Question updated. Update at the bottom-I have to put script on a page. Unfortunately, I don't have

    1天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信