Solved

javascript blocks being ignored?

Posted on 2011-03-10
4
280 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
[form-control] Retain values after a POST action 21 73
How to pass values to HTML-5 attribute dynamically? 24 66
Read text on Table 7 49
Drag & Drop Error 5 31
In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
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…

734 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