简体   繁体   English

此PHP函数的Javascript等效项

[英]The Javascript equivalent of this PHP function

So I'm trying to put together a navigation bar and I've been using PHP because I'm not familiar with Javascript. 因此,我试图将导航栏放在一起,并且由于不熟悉Javascript,所以一直在使用PHP。 I want to create a function that will return the filename of the current page I'm on and then apply css classes appropriately. 我想创建一个函数,该函数将返回当前所在页面的文件名,然后适当地应用css类。

the php code PHP代码

function setNav($section)
{
    $curSection = end(explode('/', $_SERVER['SCRIPT_NAME']));
    if ($section == $curSection)
    {
        echo ' class="active" ';
    }
}

And then I would just initialize it in the html as follows 然后我将在html中将其初始化如下

<a href="abc.def" <?php setNav('index.php'); ?> >Home </a>

For obvious reasons I'd rather do this in JavaScript or Jquery, but I'm having some trouble putting together the function. 出于明显的原因,我宁愿在JavaScript或Jquery中执行此操作,但是在将函数组合在一起时遇到了一些麻烦。 I've looked at a few tutorials, but they leave out a few things. 我看过一些教程,但它们省略了一些内容。

  1. How to pull just the last part of the file name. 如何仅提取文件名的最后一部分。 I've seen a couple ideas doing something like: 我已经看到了一些类似的想法:

     <script type="text/javascript"> var url = document.location.href; url = url.substring(0, (url.indexOf("#") == -1) ? url.length : url.indexOf("#")); url = url.substring(0, (url.indexOf("?") == -1) ? url.length : url.indexOf("?")); url = url.substring(url.lastIndexOf("/") + 1, url.length); alert(url); </script> 

    But then I run into problem 2. Also I understand how substring works but I have no idea what he's doing w/ the second argument for example '(url.indexOf("#") == -1) ? 但是后来我遇到了问题2。我也了解子字符串的工作原理,但是我不知道他在做第二个参数,例如'(url.indexOf(“#”)== -1)吗? url.length : url.indexOf("#")' I searched google, w3schools and a couple places for an explanation, also looked into advanced usage of substring() and nothing came up. url.length:url.indexOf(“#”)'我在google,w3schools和几个地方搜索了解释,还研究了substring()的高级用法,但没有发现任何问题。 Any guidance is appreciated. 任何指导表示赞赏。

  2. If say the file is the index, the url looks something like localhost/abc_corp/ so doing a document.URL call brings up "localhost/abc_corp/" and leaves out the index.php part. 如果说该文件是索引,则该URL看起来类似于localhost / abc_corp /,因此执行一个document.URL调用将显示“ localhost / abc_corp /”,并省略了index.php部分。

Any help is appreciated in advance. 任何帮助都需要提前感谢。

var scriptName = location.href.substring((location.protocol.length + location.hostname.length + 3))。split('?');

var schema = document.location.href.substring(0, document.location.href.lastIndexOf(document.domain + '/'));
var scriptName = document.location.href.replace(schema + document.domain, '');

Tested in Chrome Inspector against this same page: 在Chrome Inspector中针对同一页面进行了测试:

// document.location.href = "https://stackoverflow.com/questions/7909686/the-javascript-equivalent-of-this-php-function"

schema = "https://"
document.domain = "stackoverflow.com"
scriptName = "/questions/7909686/the-javascript-equivalent-of-this-php-function"

To solve problem 2 just if the last character in the string is a / and if so append a fake filename. 要解决问题2,即使字符串中的最后一个字符是/,如果是,请附加一个假文件名。

But I agree with Jordan - it's not obvious at all why you would do this. 但是我同意约旦的看法-根本不明白为什么要这么做。 Doing it in PHP is the correct thing. 用PHP做到这一点是正确的。

2.If there is no filename present in the URL, javascript will not know anything about the name of the file that made the response. 2.如果URL中没有文件名,则javascript将不会对作出响应的文件名称一无所知。 When localhost/abc_corp/ will result in a call to index.php this is a serverside redirect, forced by a server-setting, eg Apache's DirectoryIndex 当localhost / abc_corp /将导致对index.php的调用时,这是服务器端重定向,由服务器设置(例如Apache的DirectoryIndex)强制执行

If you have a working PHP solution I'm not sure why you want to convert it to JavaScript. 如果您有可用的PHP解决方案,则不确定为什么要将它转换为JavaScript。 PHP is a better place to do it anyway. 无论如何,PHP是一个更好的地方。

Anyway, to explain what your JS is doing, if you have a URL like this: 无论如何,如果您有这样的网址,请解释您的JS在做什么:

http://www.somedomain.com/ somepage.php ?someparam=234#123123 http://www.somedomain.com/ somepage.php?someparam = 234#123123

it will throw away anything after the "?" 它会在“?”之后丢掉任何东西。 and/or "#", and anything before the last "/", thus keeping just the bit that I put in bold. 和/或“#”,以及最后一个“ /”之前的任何内容,因此只保留了我加粗的部分。

If your URL does not contain any "/" characters, eg, you just have "www.somedomain.com", then it will return the whole string, it can't invent a filename for you. 如果您的URL不包含任何“ /”字符,例如,您只有“ www.somedomain.com”,则它将返回整个字符串,因此无法为您创建文件名。

How this works: first note that aString.indexOf(param) returns -1 if aSring does not contain param , otherwise it returns the index of the first instance. 工作原理:首先请注意,如果aSring不包含param ,则aString.indexOf(param)返回-1,否则返回第一个实例的索引。 So the second argument to substring is being set to one of two values depending on what indexOf finds. 因此,取决于indexOf找到的内容, substring的第二个参数被设置为两个值之一。 The following: 下列:

var url = document.location.href;
url = url.substring(0, (url.indexOf("#") == -1) ? url.length : url.indexOf("#"));

starts with the full URL, then it checks whether there is a "#" character - if there is not then the substring takes the whole string (0 through to url.length ), otherwise it takes the string up to but not including the "#". 以完整的URL开头,然后检查是否存在“#”字符-如果不存在,则substring将使用整个字符串(从0到url.length ),否则将字符串使用直到但不包括“ #”。 Then: 然后:

url = url.substring(0, (url.indexOf("?") == -1) ? url.length : url.indexOf("?"));

Same idea, but with "?". 相同的想法,但带有“?”。 If there isn't one keep the whole string, otherwise keep everything up to but not including the "?". 如果没有,则保留整个字符串,否则保留所有内容,但不包括“?”。 Then: 然后:

url = url.substring(url.lastIndexOf("/") + 1, url.length);

Takes everything from the character after the last "/" (which will be the first character ("-1 + 1") if there are none) through to the end of the string. 从最后一个“ /”之后的字符(如果没有则为第一个字符(“ -1 + 1”)到字符串末尾的所有内容。

var url = document.location.href;
url = url.substring(0, (url.indexOf("#") == -1) ? url.length : url.indexOf("#"));
url = url.substring(0, (url.indexOf("?") == -1) ? url.length : url.indexOf("?"));
url = url.substring(url.lastIndexOf("/") + 1, url.length);
alert(url);

So the first url = url.substring(0, (url.indexOf("#") == -1) ? url.length : url.indexOf("#")); 因此,第一个url = url.substring(0,(url.indexOf(“#”)== -1)?url.length:url.indexOf(“#”)); is checking to see if there is a pound symbol and if there is, it takes the substring from 0 to the position of the pound symbol if there is no pound it just takes it from 0 to the whole length (ie does nothing) 正在检查是否有一个井号,如果有,它将子串从0到井号的位置,如果没有井号,它将从0到整个长度(即不执行任何操作)

the second does the same thing with the question mark. 第二个与问号做同样的事情。

Problem two I could only solve by parsing for special cases. 问题二我只能通过解析特殊情况来解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM