2

我在这段代码中有一个外部 JS 文件:

var mobile_number_param = document.getElementById('mobile_number_param');
mobile_number_param.maxLength = 9;
mobile_number_param.readOnly = true;
var email = document.getElementById('email');
email.readOnly = true;
var user_notes = document.getElementById('user_notes');
user_notes.maxLength = 90;
var admin_notes = document.getElementById('admin_notes');
admin_notes.maxLength = 90;

现在我的目标是应用与“mobile_number_param”相关的代码,但仅当我在“reserve.php”页面上时,否则我不允许在其他区域修改我的手机号码,例如我的个人资料页面。

有人告诉我这个:


您可以通过检查 window.location.href 来识别当前 url,例如搜索 reserve.php 以了解您在预订页面上。然后应用您的代码。


不幸的是,我不是编码员,也不知道该怎么做。

有什么建议么 ?感谢您的时间...

4

3 回答 3

5
if (window.location.href.indexOf('reserve.php') != -1) {
    // do stuff for reserve.php page here
}
于 2013-03-26T21:08:52.437 回答
2

Just add the following function in your external JS File:

    function myFunction(pagename) {
        var pageurl = window.location.href;
        var pg = pageurl.split("/"); /*SPLITS THE URL ACCORDING TO DELIMINATOR "/". Eg x/y/z/reserve.php pg[0]=x,..pg[3]=reserve.php*/
        var pgname = (pg[pg.length - 1]);
        if (pagename == pgname) /*check whether the current page is reserve.php or not*/
        {
            return true;
        }
        return false;
    }

Calling the Function:

if (myFunction("reserve.php")) {
    /*Yes reserve.php page*/
} else {
    /*Not reserve.php page*/
}
于 2013-03-26T21:22:30.877 回答
-2

Server sided you could read the HTTP referer field. If it contains reserve.php include the one js else an other js.

If you prefer to use only one js you could wrap the fucionality into a function. called from reserve.php with parameter 1, from other pages parameter 0

or else.

于 2013-03-26T21:22:21.030 回答