0

This is a link to the working jsfiddle http://jsfiddle.net/akshaytyagi/SD66b/

and following is the code that I am trying to run on my website ( same as the one jsFiddle )

I have tried on two computers. What am I doing wrong?

<html>
<head>

<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>

<script type="text/javascript">

$(document).ready(function() {

var tips = [
    "creative",
    "innovative",
    "awesome",
    "amazing",
    "social"
    ];

setInterval(function() {
    var i = Math.round((Math.random()) * tips.length);
    if (i == tips.length)--i;

    $('#tip').slideUp(500, function() {
        var $this = $(this);
        $this.html(tips[i]);
        $this.toggleClass('first second');
        $this.slideDown(500);
    });

}, 3 *1000);

});
</script>
</head>

<body>
<div style=" background-position:center; background-repeat:no-repeat; background-color:#c84d5f; height:500px">
<div class="thousand">
<div style="font-size:72px; font-family:Museo; padding-top:100px; padding-left:auto; padding-right:auto; color:#FFF;">The <span id="tip">creative</span><br />brand.
</div>
</div>
</div>


</body>
</html>
4
  • 2
    Did you check the JavaScript console for any errors? Commented Nov 9, 2012 at 14:29
  • 2
    Yes, the <head> should be before the <body>, not inside it. Commented Nov 9, 2012 at 14:31
  • You have not described the ways in which the code doesn't work for you. It's not working very well even in the jsfiddle, really; the effect is jumpy. Commented Nov 9, 2012 at 14:34
  • Prisoner : Thanks, fixed it now. @Pointy The animation doesn't work at all. Commented Nov 9, 2012 at 14:48

4 Answers 4

8

You need to put the script which access DOM element in $(document).ready to make sure the elements are ready before they are accessed.

$(document).ready(function(){

})

Edit based on comments

Change

<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>

To

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2jquery.min.js"></script>
Sign up to request clarification or add additional context in comments.

5 Comments

I don't think that'd make any difference here, though it's not a bad idea.
You are right in general, but because the code is inside setInterval I don't think that's the problem.
Taking a second look you are right because setInterval was called when page was loading
The first run of that script wont be run until 3 seconds, after the dom is loaded.
Change <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script> To <script type="text/javascript" src="ajax.googleapis.com/ajax/libs/jquery/1.8.2/…> and tell me if it helps?
7

I copy and pasted your HTML, and after }, 3 * 1000); there is a special char.

Delete that whole line (}, 3 * 1000);) and re-type it.

See:

here

As andyb has commented, if you're loading the file locally your jquery url wont work. You can either change it to http:// or upload your file somewhere.

14 Comments

Ha ha this happens so much; I've got an old answer just like your's that's gotten a zillion upvotes over the past couple years :-) It's a jsFiddle side-effect I think.
Nice find, I can confirm that this fixes the issue.
I copied the code, pasted into Sublime Text, saved, opened in Chrome and got the same error. It does not seem to be just jsFiddle related.
I'm assuming he wrote this in jsfiddle, then moved it into his own html file.
@AkshayTyagi It works if the page was being loaded from a web server. If you just open the .html file in a browser it will not be able to resolve the link - see stackoverflow.com/questions/4831741/…
|
1

Although the right answer was already given, I've taken the liberty to fix your markup. And may I suggest you use proper CSS instead on inline styling? It makes your code much more readable and separates markup and design as you should,

<html><head>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
    <script>
    var tips = [
        'creative',
        'innovative',
        'awesome',
        'amazing',
        'social'
    ];

    setInterval(function() {
        var i = Math.round((Math.random()) * tips.length);
        if (i == tips.length)--i;

        $('#tip').slideUp(500, function() {
            var $this = $(this);
            $this.html(tips[i]);
            $this.toggleClass('first second');
            $this.slideDown(500);
        });
    }, 3000);
    </script>
</head><body>
    <div style="background-position:center; background-repeat:no-repeat; background-color:#c84d5f; height:500px">
        <div class="thousand">
            <div style="font-size:72px; font-family:Museo; padding-top:100px; padding-left:auto; padding-right:auto; color:#FFF;">
                The <span id="tip">creative</span><br />brand.
            </div>
        </div>
    </div>
</body></html>

1 Comment

Thanks. I usually always clean up the code, but right now was just trying to make this work.
0

The problem making it work, locally, was that // links do not get resolved to http:// ( src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js instead of just src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js")

[ Thanks to @andyb : I was wondering why Google had improper code on their site. ]

1 Comment

The protocol-less src works if the page was being loaded from a web server. If you just open the .html file in a browser it might not be able to resolve the link - see stackoverflow.com/questions/4831741/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.