Solved

javascript blocks being ignored?

Posted on 2011-03-10
4
275 Views
Last Modified: 2012-08-14
If I put 2 javascript blocks inside an html page, the 1st js block gets ignored and the 2nd one gets executed. Like so:

<script language="javascript" type="text/javascript">
   alert("alert #1")
</script>

<script language="javascript" type="text/javascript">
   alert("alert #2")
</script>

Open in new window



When I load the page, I only see "alert #2". Can someone confirm this behavior is real? The reason I have 2 js blocks and not just 1 is because the 1st one is part of my asp.net master page, and the 2nd is reserved for individual pages. The master page gets merged at runtime with individual pages (like an include/header idea), so both js blocks end up on the page together.

Note: The jquery library is referenced and is being invoked further down the page. Could this be impacting things?

I am using IE8.

Any ideas?
0
Comment
Question by:SweatCoder
  • 2
4 Comments
 
LVL 29

Expert Comment

by:Badotz
Comment Utility
You are depending on the page to execute your JavaScript when it wants to.

You shoul instead wrap your code like this:


<script type="text/javascript">

function page_load() {
    
    alert("alert #1");
    alert("alert #2");
}

window.onload = page_load;

</script>

Open in new window

0
 
LVL 29

Expert Comment

by:Badotz
Comment Utility
Or, since you use jQuery, use the "ready" syntax (visit the jQuery home page for details).
0
 
LVL 20

Accepted Solution

by:
Proculopsis earned 500 total points
Comment Utility

It looks like .net is getting in the way, for reliable execution of JavaScript, use one of the following methods in your code behind:

  .RegisterClientScriptBlock()
  .RegisterStartupScript()
0
 
LVL 11

Author Closing Comment

by:SweatCoder
Comment Utility
This solved my problem! Just a side note though, those methods are depracated. Here is the reference for the replacement:

http://msdn.microsoft.com/en-us/library/system.web.ui.clientscriptmanager.aspx
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now