Solved

javascript blocks being ignored?

Posted on 2011-03-10
4
276 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
ID: 35100890
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
ID: 35100900
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
ID: 35106438

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
ID: 35109453
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
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…

863 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

23 Experts available now in Live!

Get 1:1 Help Now