www.webdeveloper.com
Results 1 to 3 of 3

Thread: What is wrong with my javascript code format ?

  1. #1
    Join Date
    Oct 2009
    Posts
    71

    What is wrong with my javascript code format ?

    The code is working. I have a timer count down and under it a scrolling text.
    But i have been told that:

    I have malformed (invalid) HTML. You cannot have <script> tags as immediate children of <ul>. Only <li> elements can be directly under <ul>. See: developer.mozilla.org/en-US/docs/Web/HTML/Element/ul

    And also:

    To make sure that the Javascript loaded the function first before calling it. Your news() and timer() function was called before declaring it. And AVOID, as much as possible, to put <script> tag inside an element aside from <header> and before </html>. Be sure to always Optimize your code.

    This is my code in my website:

    Code:
    <script>
    var count = 300;
    var counter = setInterval(timer, 1000); //1000 will  run it every 1 second
    
    function timer() {
        count = count - 1;
        if (count == -1) {
            clearInterval(counter);
            return;
        }
    
        var seconds = count % 60;
        var minutes = Math.floor(count / 60);
        var hours = Math.floor(minutes / 60);
        minutes %= 60;
        hours %= 60;
    
        document.getElementById("timer").innerHTML = hours + " Hours " + minutes + " Minutes and " + seconds + " Seconds left untill the next news update. "; // watch for spelling
    }
    </script>
    <span id="timer"></span>
    <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
    <script src="http://newsxpressmedia.com/files/theme/jquery.newsTicker.js"></script>
    <script>
    $(function() {
        news();
        setInterval(function(){
          news()
        },30000)  // it will call every 1 min you can change it
    });
    </script>
    <div id="oneliner">
        <div class="header"> Breaking News </div>
        <ul class="newsticker">
    <script>
    function news(){
       $('body').find('.newsticker').remove();//It will clear old data if its present 
       var file = "http://newsxpressmedia.com/files/theme/test.txt";
        $.get(file, function (txt) {
            //var lines = txt.responseText.split("\n");
            var lines = txt.split("\n");
            $ul = $('<ul class="newsticker" />');
            for (var i = 0, len = lines.length; i < len; i++) {
                //save(lines[i]); // not sure what this does
                $ul.append('<li>' + lines[i] + '</li>'); //here 
            }
            //$ul.appendTo('body').newsTicker({
            $ul.appendTo('div.wcustomhtml').newsTicker({
                row_height: 48,
                max_rows: 2,
                speed: 6000,
                direction: 'up',
                duration: 1000,
                autostart: 1,
                pauseOnHover: 1
            });
        });
    }
    </script>
    </ul>
    </div>

    Could someone show me how the code should be and look like ?

    Thank you.

  2. #2
    Join Date
    Mar 2005
    Location
    Behind you...
    Posts
    1,033
    I don't know if this is what you want, but you could rearrange your code to look like this:
    HTML Code:
    <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
    <script src="http://newsxpressmedia.com/files/theme/jquery.newsTicker.js"></script>
    <script>
    	var count = 300;
    	var counter = setInterval(timer, 1000); //1000 will  run it every 1 second
    
    	function timer() {
        count = count - 1;
        if (count == -1) {
    			clearInterval(counter);
    			return;
        }
    
        var seconds = count % 60;
        var minutes = Math.floor(count / 60);
        var hours = Math.floor(minutes / 60);
        minutes %= 60;
        hours %= 60;
    
        document.getElementById("timer").innerHTML = hours + " Hours " + minutes + " Minutes and " + seconds + " Seconds left untill the next news update. "; // watch for spelling
    	}
    	
    	function news(){
       $('body').find('.newsticker').remove();//It will clear old data if its present 
       var file = "http://newsxpressmedia.com/files/theme/test.txt";
        $.get(file, function (txt) {
    			//var lines = txt.responseText.split("\n");
    			var lines = txt.split("\n");
    			$ul = $('<ul class="newsticker" />');
    			for (var i = 0, len = lines.length; i < len; i++) {
    				//save(lines[i]); // not sure what this does
    				$ul.append('<li>' + lines[i] + '</li>'); //here 
    			}
    			//$ul.appendTo('body').newsTicker({
    			$ul.appendTo('div.wcustomhtml').newsTicker({
    				row_height: 48,
    				max_rows: 2,
    				speed: 6000,
    				direction: 'up',
    				duration: 1000,
    				autostart: 1,
    				pauseOnHover: 1
    			});
        });
    	}
    	$(function() {
        news();
        setInterval(function(){
          news();
        },30000)  // it will call every 1 min you can change it
    	});
    </script>
    <span id="timer"></span>
    
    <div id="oneliner">
    	<div class="header"> Breaking News </div>
    	<ul class="newsticker"></ul>
    </div>
    That should get rid of any invalid/malformed HTML issues and it functions the same way.

  3. #3
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,506
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

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