Results 1 to 5 of 5

Thread: script works IE not FireFox, help?

  1. #1
    Join Date
    Nov 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default script works IE not FireFox, help?

    <head>
    <script type="text/javascript" language="javascript">
    var wlen;
    var setIntID;
    setIntID = 0;
    wlen = 10;
    function SetitUp() {
    setIntID = setInterval("Pic_load()", 1)
    }

    function Pic_load() {
    wlen = wlen + 4;
    if (wlen < 399) {
    document.getElementById("pictureDiv").style.width = +wlen;
    }
    if (wlen > 398) {
    clearInterval(setIntID);
    }
    }

    </script>
    <link href="StyleSheet.css" rel="stylesheet" type="text/css" />
    <title></title>
    </head>
    <body >
    <div id="pictureDiv" onload="Pic_load();" >
    <script type="text/javascript" language="javascript">
    window.onload = SetitUp();
    </script>
    <img alt='' src='http://www.killcravings.com/Gallery/albums/userpics/normal_DSCN1155.JPG' />


    </div>

    </body>
    Last edited by markakapops; 11-12-2009 at 08:51 PM. Reason: show resolved

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    You have invalid code and markup. Also the code is sloppy. Try:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <style type="text/css">
    #pictureDiv {
     width: 10px;
    }
    #pictureDiv img {
     width: 100%;
    }
    </style>
    <script type="text/javascript">
    (function(){
    	var wlen = 10, setIntID, picDivStyle;
    	function SetitUp(){
    		picDivStyle = document.getElementById('pictureDiv').style;
    		setIntID = setInterval(Pic_load, 20);
    	}
    	function Pic_load(){
    		wlen += 10;
    		if (wlen < 399){
    			picDivStyle.width = wlen + 'px';
    		}
    		else{
    			picDivStyle.width = '398px';
    			clearInterval(setIntID);
    		}
    	}
    	window.onload = SetitUp;
    })();
    </script>
    </head>
    <body>
    <div id="pictureDiv">
    <img alt="" src="http://www.killcravings.com/Gallery/albums/userpics/normal_DSCN1155.JPG">
    </div>
    </body>
    </html>
    Last edited by jscheuer1; 11-09-2009 at 10:42 PM. Reason: minor code improvement
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Nov 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Interesting

    John I am seld taught, and an old dog, I am not sure why you made the changes you made and why? Can you recommend a book or a sight that might explain these things simply? I have been using http://www.w3schools.com/ mostly. It looks like you wraped the entire thing in an empty function and )(); is at the end , and to be honest I do not have clue as to why.

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    That's a self executing anonymous function. Using that in this case keeps the global scope free of our variables. This is simply good practice, as it avoids many potential conflicts with other scripts. However, it makes our variables and their values unavailable outside of the anonymous function. So for more complex code generally one variable is defined in the global scope, usually an object of some type (function, array or object), through which any required value of the code may be accessed.

    I'm also self taught. Unfortunately I cannot wholeheartedly recommend any resource either online or book. One can learn a lot from various resources though. One book, by no means perfect, that I've found very helpful is:

    Object-Oriented JavaScript
    by: Stoyan Stefanov
    publisher: PACKT

    But the way you are learning is how I started out. I think I gained the most from working with existing code, tweaking it for specific purposes, and by hanging out here helping folks, rubbing elbows and sometimes even knocking heads with other coders.

    There is no one way to write javascript code. But there are numerous good practices that can be incorporated into your code in various ways. And there is a tremendous amount of information on how to do this and that available, not all of which is all that well thought out nor necessarily all that current. Differences in browsers often must be taken into account, but less so once you know more of what you are doing. And it is best to have your code branch on what a browser can do, rather than on what 'make or model' it is.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    Nov 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thank you for taking the time to explain why you made the changes you made. I have ordered the book.
    Thanks again for your help.

Bookmarks

Posting Permissions

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