Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Apr 2009
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question One action, many event handlers (again) "resolved"

    Hi everyone,
    I guess this may have been a question before but I couldn't search for it because the term "if" was too small...

    So I'm a really big noob, started javascript today, looked everywhere and ended up here.

    I have this javascript,
    Code:
    if (document.getElementById) { window.onload = swap };
    	function swap() {
    	var numimages=2;
    	rndimg = new Array("./Images/image_01.jpg", "./Images/image_02.jpg"); 
    	x=(Math.floor(Math.random()*numimages));
    	randomimage=(rndimg[x]);
    	document.getElementById("image_01").style.backgroundImage = "url("+ randomimage +")"; 
    }
    if (document.getElementById) { window.onload = swap };
    	function swap() {
    	var numimages=2;
    	rndimg = new Array("./Images/image_03.jpg", "./Images/image_04.jpg"); 
    	x=(Math.floor(Math.random()*numimages));
    	randomimage=(rndimg[x]);
    	document.getElementById("image_02").style.backgroundImage = "url("+ randomimage +")";
    }
    But the problem is, the second if gets called the last in the html and so the first one isn't used when displayed (Hope I can explain it clearly)

    So how can I get rid of the "if" things?

    Oh BTW, the lines who uses javascript in my html file are
    Code:
    <a href="./iSoftware.html" id="image_01"></a>
    <a href="./Contribute.html" id="image_02"></a>
    and CSS:
    Code:
    #image_01		{
    		position: absolute;
    		background-image:url("../Images/image_01.png);
    		top:105;
    		left:52;
    		width:249px; 
    		height:150px;
    		float:left;
    		}
    #image_02		{
    		position: absolute;
    		background-image:url("../Images/image_03.png);
    		top:105;
    		left:302;
    		width:249px; 
    		height:150px;
    		float:left;
    		}
    Thanks
    Last edited by JamesGreen; 04-19-2009 at 10:38 AM. Reason: I screwed it up, sorry

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,984
    Thanks
    203
    Thanked 2,536 Times in 2,514 Posts
    You cannot have two functions swap() with the same name as the seond overwrites the first. I think you have taken a script and simply duplicated it to change a different set of images - that will not work unless you rename the second function and all the global variables within it.

    Nor may you have multiple window.onload statements as once again the second will over-write the first. For more info see:-

    http://www.javascriptkit.com/javatut...iplejava.shtml

    Do please read the posting guidelines regarding silly thread titles. The thread title is supposed to help people who have a similar problem in future. Yours is useless for this purpose.

    Not sure why you have posted this Javascript question in this JSON forum.


    "A little sincerity is a dangerous thing, and a great deal of it is absolutely fatal". - Oscar Wilde (Irish Poet, Novelist, Dramatist and Critic, 1854-1900)

  • Users who have thanked Philip M for this post:

    JamesGreen (04-19-2009)

  • #3
    New to the CF scene
    Join Date
    Apr 2009
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Not sure why you have posted this Javascript question in this JSON forum.
    Thought I needed to be placed in the DOM section, well, now I guess the "JavaScript frameworks" forum. Admin ?

    Thanks for your answer and sorry for all the problems I started. I shouldn't deserve an answer after all I caused...

    Sorry
    Last edited by JamesGreen; 04-19-2009 at 10:28 AM.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,984
    Thanks
    203
    Thanked 2,536 Times in 2,514 Posts
    You can edit the title yourself.

    It is nothing to do with Javascript Frameworks. JavaScript Frameworks
    forum is for people wanting help with one of the popular JS frameworks, such as Prototype, jQuery, YUI etc. The correct forum here is Javascript Programming. Ask a mod to move you over. They are listed at the bottom of the main threads page.

  • #5
    New to the CF scene
    Join Date
    Apr 2009
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    You can edit the title yourself.

    It is nothing to do with Javascript Frameworks. JavaScript Frameworks
    forum is for people wanting help with one of the popular JS frameworks, such as Prototype, jQuery, YUI etc. The correct forum here is Javascript Programming. Ask a mod to move you over. They are listed at the bottom of the main threads page.
    Thanks for that, I've changed the title and PM'd a Client side development moderator..

    Haven't got my Javascript working but now that I have discovered that it's a very common question, I'll work it out somehow because there's lots of documentation.


  •  

    Tags for this Thread

    Posting Permissions

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