Reputation: 12000
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.
Upvotes: -1
Views: 227
Reputation: 10598
try wrapping your code inside ready
block:
$(document).ready(function () {
//your code
});
Upvotes: 0
Reputation: 10177
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.
Upvotes: 2
Reputation: 324600
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);
Upvotes: 4