2

consider the following javascript

var str=window.location;
newArray=str.split('/');
document.write(newArray[0]);

When I execute split on "var url" which holds "window.location" it is causing this error in firebug console

TypeError
arguments: Array[2]
message: "—"
stack: "—"
type: "undefined_method"
__proto__: Error

3 Answers 3

3

What you need is the string that contains the url.

window.location.href
Sign up to request clarification or add additional context in comments.

Comments

1

window.location is an object. If you want just the URL, use window.location.href.

Comments

0

window.location is an Object (so it has no split() method). split() is a method of a String.

You need to use window.location.href or window.location.toString() (or use it in a String context so its coerced to a String).

You may also be wanting to split on the path only, in which case you should use location.pathname. You can then use substr(1) to drop the leading / (which would only split to an empty string anyway with split('/')).

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.