1

I'm trying to check for two different hash substrings and execute different code depending. This is what I have right now:

Javascript

if(window.location.hash.substring(confirm) {
    $('.confirm').click();  
}

elseif(window.location.hash.substring(thanks) {
    $('.thanks').click();
}

Any idea what I am doing wrong?

4

1 回答 1

3

Use indexOf with quotes to denote the string you want to search:

if(window.location.hash.indexOf('confirm') >= 0) {
    $('.confirm').click();  
}
else if(window.location.hash.indexOf('thanks') >= 0) {
    $('.thanks').click();
}

BTW, in your original implementation, you were also missing ) in the if condition.

于 2012-04-16T18:12:54.453 回答