Solved

javascript blocks being ignored?

Posted on 2011-03-10
4
277 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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…

772 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