www.webdeveloper.com
Results 1 to 8 of 8

Thread: Javascript NOT WORKing

  1. #1
    Join Date
    Jul 2009
    Posts
    73

    Javascript NOT WORKing

    Strangest thing....i have a Sharepoint site where all of my pages are running javascript on them however if i attempt to add additional javascript to my main page it will not execute. and im using simple code like below. Has anyone ever experienced this before....?

    window.onload = function(){
    alert("hello");
    }

  2. #2
    Join Date
    Dec 2005
    Posts
    2,984
    Is there a Javascript error elsewhere on the page? If there is, Javascript parsing and execution will be stopped so this event listener will never be applied.
    I've switched careers...
    I'm NO LONGER a scientist,
    but now a web developer...
    awesome.

  3. #3
    Join Date
    Feb 2006
    Posts
    2,926
    Apparently you are either overiding an existing onload event, or your new onload is itself overwritten before the document loads.

  4. #4
    Join Date
    Mar 2009
    Posts
    430
    Where are you adding this additional script? For that matter, where does the original script come from? Does it/them come from an external file, or is it located within the head section, or (shudder) within the body of the document? If you can give a truncated HTML and javascript example of this you probably can get a solution. But right now there is insufficient data for the folks who are willing to help you to do much more than speculate on the cause of your problem. Post an example.

  5. #5
    Join Date
    Jul 2009
    Posts
    73
    I wish i could give you an example but i wouldn't even want you to have to sifen through that much code. But i am simply placing this code in the heading of my page. It not only window.onload but any new code will not execute. At first i was thinking someone was playing a cruel joke on me but since im able to write script to the other pages i know it's not a joke.

    To answer all of your questions

    AJ NSC = NO other errors on the page and it so, they are not being thrown

    mrhoo = not limited to onload event. This was just a simple example of code that should work.

    Tcobb = this code is being written in the head of my document. i have some external JS files but this code is being writting directly inline. But i have tried both and neither are executing.

  6. #6
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Apparently you have another window.onload within the other external JS files. But javascript events can not be fired more than once on the same element.

    You should probably add a new function on the same event. Could be:
    Code:
    <script type="text/javascript">
    // START cross-browser attach events functions
    function AttachEvent(obj,evt,fnc,useCapture){
    	if (!useCapture) useCapture=false;
    	if (obj.addEventListener){
    		obj.addEventListener(evt,fnc,useCapture);
    		return true;
    	} else if (obj.attachEvent) return obj.attachEvent("on"+evt,fnc);
    	else{
    		MyAttachEvent(obj,evt,fnc);
    		obj['on'+evt]=function(){ MyFireEvent(obj,evt) };
    	}
    } 
    function MyAttachEvent(obj,evt,fnc){
    	if (!obj.myEvents) obj.myEvents={};
    	if (!obj.myEvents[evt]) obj.myEvents[evt]=[];
    	var evts = obj.myEvents[evt];
    	evts[evts.length]=fnc;
    }
    
    function MyFireEvent(obj,evt){
    	if (!obj || !obj.myEvents || !obj.myEvents[evt]) return;
    	var evts = obj.myEvents[evt];
    	for (var i=0,len=evts.length;i<len;i++) evts[i]();
    }
    // END cross-browser attach events functions
    
    // Add a new function onload
    AttachEvent(window,'load',function(){alert('hello')},false);
    </script>

  7. #7
    Join Date
    Jul 2009
    Posts
    73
    You have got to be kidding me. In the Body tag of my page i had an onload event which was stoping my script from running.

  8. #8
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Quote Originally Posted by uknowmeim View Post
    You have got to be kidding me. In the Body tag of my page i had an onload event which was stoping my script from running.
    That means you've got to be kidding us. We all asked you to check that

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles