无法在javascript中获取字符串长度

发布于 2024-11-17 20:58:28 字数 928 浏览 1 评论 0原文

就这件事花了1个多小时。这是我的index.php 文件中的javascript 代码。

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);
}

症状:前两个警告框没有出现问题,并且都显示“thepath”中有一个有效的路径名,而且它也是正确的、预期的路径。

但无论我尝试什么——查看 4 种不同的尝试,一次尝试一个——最后的 Alert() 框永远不会出现。 为什么alert('pathLen is ' + pathLen)没有显示?

(另一件事是 - 我正在使用 XDEBUG、xampp 和 Netbeans,调试器不会让我在这段 javascript 代码中放置断点,所以我什至无法单步进入它来查看内容发生了,因此在代码中使用alert()我知道我在Netbeans中使用的XDEBUG调试器是有效的——我整夜都在使用它来调试不同的.PHP文件中的PHP代码。我设定尽管在任何 Javascript 代码中都有一个断点,但都会出现“断点”图标,而且我在 Netbeans 文档中找不到这意味着什么。)

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.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

失与倦" 2024-11-24 20:58:28

我以前从未见过这种语法。您可能想尝试:(

var pathLen = thepath.length;

最好使用 Firebug 进行调试)

I've never seen that syntax before. You might want to try:

var pathLen = thepath.length;

(You'd be best off debugging with Firebug)

另类 2024-11-24 20:58:28
var pathLen = thepath.length;

长度是字符串的属性,而不是函数,因此不需要 ()

var pathLen = thepath.length;

Length is a property of the string, not a function, so no need for the ().

雨后彩虹 2024-11-24 20:58:28

长度是字符串的属性而不是方法。
您应该能够通过以下方式访问它:

var pathLen = thepath.length;

当您说警报框永远不会出现时,您的意思是它根本不会出现吗?如果您使用 FF,您可以从“工具”菜单打开错误控制台并清除它,然后刷新页面。它应该突出显示代码中存在的任何 JS 错误。这是我知道警报根本不会显示的唯一原因。 (我不认为 String.length() 有一个类方法,这可能是错误的来源。)

至于 XDebug,据我所知它是一个 PHP 调试器,只是我不认为它可以调试JS。

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 coming from.)

As for XDebug, as far as I know it's a PHP debugger only I don't think it can debug JS.

你没皮卡萌 2024-11-24 20:58:28
pathLen.length

没有 ()。长度是一个属性;如果添加 (),它会尝试使用属性的值作为函数来调用,从而导致异常。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文