Javascript + and string return value from function?
This is a stupid noob issue that just pisses me off .. sorry.
- This works:
alert('hello');
- This works:
alert(getsomestring());
- This works:
alert('hello'+'goodbye')
- So why doesn't this work:
alert('hello'+getsomestring());
I tried these with no luck:
alert('hello'+getsomestring(););
alert('hello'+getsomestring().toString(););
thanks.
using it as follows:
<script language="JavaScript">
function getQueryString() {
var assoc = new Array();
var queryString = unescape(location.search.substring(1));
var keyValues = queryString.split('&');
for (var i in keyValues) {
var key = keyValues[i].split('=');
assoc[key[0]] = key[1]; 开发者_运维技巧
}
return assoc;
}
</script>
<script language="JavaScript">
alert('?h='+getQueryString()["search"];);
//localSearchHighlight('?h='+getQueryString()["search"]);
);
</script>
Your code has syntax errors, in the second <script>
block. It should read:
alert('?h='+getQueryString()["search"]);
- There was a
;
inside the function call parens. - You had an additional line with
);
after your comment.
Other than that, it seems to work like you want it too.
Take a look in your browser's error console; alert('hello'+getsomestring();)
is a syntax error due to the semicolon. Semicolons separate lines and shouldn't appear in expressions. Remove it, and the expressions will work (as you have it typed at first: alert('hello'+getsomestring());
).
There are no associative arrays in JavaScript. Only objects map keys to values.
Replace var assoc = new Array();
with var assoc = new Object();
or the shorthand var assoc = {};
then it works.
Also there's an superflous ;
in your alert:
alert('?h='+getQueryString()["search"];);
^ --------- SyntaxError: Unexpected token ;
精彩评论