Over 1 hour on this. This is javascript code inside my index.php file.
function dostuff()
{
var thepath = document.location.search.substring(1);
alert('the path is ' + thepath + " (that's the full path)");
alert(thepath);
// TRIED THESE ALL -- NONE OF THEM WORK.
//var pathLen = String.length("thepath");
//var pathLen = String.length(thepath);
//var pathLen = thepath.length();
var pathLen = String.length(document.location.search.substring(1));
alert('pathLen is ' + pathLen);
}
The symptom: the first 2 alert boxes appear no problem and both show 'thepath' has a valid pathname in it, and it is the correct, expected path too.
But no matter what I try -- see the 4 different attempts, tried one at a time -- the final alert() box NEVER shows up. Why is alert('pathLen is ' + pathLen) not showing up?
(The other thing is -- I'm using XDEBUG and xampp and Netbeans and the debugger will not let me put a breakpoint in this javascript code, so I can't even step into it to see what's happening, hence the use of the alert()'s in the code. I know the XDEBUG debugger I'm using in Netbeans works -- I've been using it all night to debug PHP code in a different.PHP file. When I set a breakpoint though in any Javascript code, a 'broken breakpoint' icon appears and I cannot find what that means in Netbeans documentation.)
Over 1 hour on this. This is javascript code inside my index.php file.
function dostuff()
{
var thepath = document.location.search.substring(1);
alert('the path is ' + thepath + " (that's the full path)");
alert(thepath);
// TRIED THESE ALL -- NONE OF THEM WORK.
//var pathLen = String.length("thepath");
//var pathLen = String.length(thepath);
//var pathLen = thepath.length();
var pathLen = String.length(document.location.search.substring(1));
alert('pathLen is ' + pathLen);
}
The symptom: the first 2 alert boxes appear no problem and both show 'thepath' has a valid pathname in it, and it is the correct, expected path too.
But no matter what I try -- see the 4 different attempts, tried one at a time -- the final alert() box NEVER shows up. Why is alert('pathLen is ' + pathLen) not showing up?
(The other thing is -- I'm using XDEBUG and xampp and Netbeans and the debugger will not let me put a breakpoint in this javascript code, so I can't even step into it to see what's happening, hence the use of the alert()'s in the code. I know the XDEBUG debugger I'm using in Netbeans works -- I've been using it all night to debug PHP code in a different.PHP file. When I set a breakpoint though in any Javascript code, a 'broken breakpoint' icon appears and I cannot find what that means in Netbeans documentation.)
Share Improve this question asked Jun 30, 2011 at 8:31 wantTheBestwantTheBest 1,7204 gold badges46 silver badges71 bronze badges 5-
What makes you think
String.length
works? See: developer.mozilla/en/JavaScript/Reference/Global_Objects/… – Felix Kling Commented Jun 30, 2011 at 8:40 - @Felix -- I based it on a sample online that used String.length() to get the length of a string in Javascript -- are you saying that the javascript String.length() function does not work or does not exist? – wantTheBest Commented Jun 30, 2011 at 8:54
- It does not exist at all... where did you find that example? – Felix Kling Commented Jun 30, 2011 at 8:55
- 1 No worries.... post whenever you want/can, there is no rush. In any case, www.w3schools. is not a good reference. Use MDN instead: developer.mozilla/en/JavaScript/Reference – Felix Kling Commented Jun 30, 2011 at 9:09
- +1 @Felix, thanks for that, w3schools has helped me out, I'm such an amateur any knowledgebase is good for me, but as I've gone along these past 2 weeks, more and more I've said 'not enough info' when looking for help in w3schools -- have now bookmarked your link, thanks! – wantTheBest Commented Jun 30, 2011 at 17:09
4 Answers
Reset to default 6I've never seen that syntax before. You might want to try:
var pathLen = thepath.length;
(You'd be best off debugging with Firebug)
var pathLen = thepath.length;
Length is a property of the string, not a function, so no need for the ()
.
The length is a property of your string rather than a method. You should be able to access it via the following:
var pathLen = thepath.length;
When you say the alert box never shows up do you mean it never appears at all? If you're using FF you can open the error console from the Tools menu and clear it then refresh your page. It should highlight any JS errors you have in your code. That's the only reason I know of that the alert wouldn't show at all. (I don't think there is a class method for String.length() which is probably where the error is ing from.)
As for XDebug, as far as I know it's a PHP debugger only I don't think it can debug JS.
pathLen.length
No ()
. length is a property; if you add the (), it tries to use the value of the property as a function to call, resulting in an exception.