最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Execute javascript only in specific url - Stack Overflow

programmeradmin0浏览0评论

I have an external JS file with inside this code:

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;

Now my goal is to apply the code related to "mobile_number_param" but ONLY when I'm on the "reserve.php" page otherwise I'm not allowed to modify my Mobile Phone number in other area such my profile page.

Somebody told me this:


You can recognize the current url by checking window.location.href and e.g. searching for reserve.php to know you're on the reservation page..then apply your code.


Unfortunately I'm not a coder and don't have any idea how to do.

Any suggestions ? Thank for your time...

I have an external JS file with inside this code:

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;

Now my goal is to apply the code related to "mobile_number_param" but ONLY when I'm on the "reserve.php" page otherwise I'm not allowed to modify my Mobile Phone number in other area such my profile page.

Somebody told me this:


You can recognize the current url by checking window.location.href and e.g. searching for reserve.php to know you're on the reservation page..then apply your code.


Unfortunately I'm not a coder and don't have any idea how to do.

Any suggestions ? Thank for your time...

Share Improve this question asked Mar 26, 2013 at 21:07 dot22dot22 2591 gold badge8 silver badges20 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5
if (window.location.href.indexOf('reserve.php') != -1) {
    // do stuff for reserve.php page here
}

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*/
}

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.

发布评论

评论列表(0)

  1. 暂无评论