2

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

3 Answers 3

5
if (window.location.href.indexOf('reserve.php') != -1) {
    // do stuff for reserve.php page here
}
Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

hehe, welcome. There is not a much difference.The above solution use javascript inbuilt function for checking a sub-string in a string while I had written my own javascript function for the same. That's the only difference.:)
-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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.