Execute javascript only in specific url - Stack Overflow

I have an external JS file with inside this code:var mobile_number_param = document.getElementById(

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745289779a4620785.html

相关推荐

  • Execute javascript only in specific url - Stack Overflow

    I have an external JS file with inside this code:var mobile_number_param = document.getElementById(

    4小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信