-1

I keep getting the "Syntax Error: Unexpected identifier" JS error with this code:

function hashStuff() {
    var messageID = window.location.hash.replace('#inbox-', '');
    var msgSubject = $('#subject_' + messageID).html();
    setTimeout("readMessage2(" + messageID + ", " + msgSubject + ");", 300);
}
if (window.location.hash) {
    setTimeout("hashStuff();", 400);
}

I've also tried:

if (window.location.hash) {
    function hashStuff() {
    var messageID = window.location.hash.replace('#inbox-', '');
    var msgSubject = $('#subject_' + messageID).html();
    setTimeout("readMessage2(" + messageID + ", " + msgSubject + ");", 300);
}
    setTimeout("hashStuff();", 400);
}

Neither of them work.

What I was trying to do was get information from the elements but I guess the page wasn't loaded yet so I need it to trigger after a second. I put it in a function so I can use a timeout and it will not work.

Any ideas? Thanks in advance.

7
  • Do you know what line is causing the error? Commented Oct 25, 2011 at 22:16
  • avoid using timers, very inconsistent, just use the jQuery ready Commented Oct 25, 2011 at 22:18
  • To the person who downvoted: thanks for commenting about why you downvoted! :| Commented Oct 25, 2011 at 22:20
  • Your title kind of sucks. You are creating a string to eval using setTimeout - I bet those parameters need quotes. Commented Oct 25, 2011 at 22:29
  • 3
    @Nathan well you could maybe figure out the line of code with the syntax error, take a guess at the part of the line that's causing it, and make up a title like "Why does 'foo..bar' cause a syntax error?" Even if "foo..bar" isn't the cause, it's still more specific that, essentially, "Halp!!". Commented Oct 25, 2011 at 22:57

3 Answers 3

4

If your messageID is something like 1234 and the msgSubject is Hello World, then the statement being evaluated is:

readMessage2(1234, Hello World);

Which, clearly, is incorrect and error-inducing.

The correct code is:

setTimeout( function() {readMessage2(messageID,msgSubject);}, 300);
Sign up to request clarification or add additional context in comments.

Comments

2

You can run the script inside $(document).ready(function() {//script here}); . That will make sure that it is run after all the elements have loaded.

3 Comments

Yeah, I'm surprised I didn't think of that. Thanks!
But another problem is that it keeps repeating. So it keeps repeatedly calling the readMessage2() function now with document.ready over and over and over again. What part was I supposed to put in the .ready, the whole if() or just a part of it?
@Nathan You were supposed to put the code you wanted to run in the first place (get messageID and msgSubject and call readMessage2() I guess). No need for setTimeout() if I understood correctly.
0

try wrapping your code inside ready block:

$(document).ready(function () {
    //your code
});

Comments

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.