PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, July 15, 2022

[FIXED] How do I detect a specific page in a js script so that I can return from the function?

 July 15, 2022     javascript, web-deployment     No comments   

Issue

I don't think my question needs much more context but the basic problem I'm facing is that I have a script running to hide the nav bar, simple stuff. I have a page that has very little content but it's enough to have a small scroll and due to this, the page looks ugly if a user scrolls.

I tried:

if(window.location.pathname == "/page.php") {
  return;
} else {
  //code...
}

But it's not working for some reason. The else block is still running. My URL is something like localhost/foldername/page.php?data=something

I'm using xampp and no frameworks as I've just started learning Web Development last week so any solutions that do not include any frameworks is appreciated. I will learn frameworks, just not now


Solution

If the url is http://localhost/foldername/page.php?data=something then the pathname will be:

/foldername/page.php

So try just checking for that filename

if(window.location.pathname.includes("/page.php")) {
   //code...

}

Or use the full pathname

if(window.location.pathname === '/foldername/page.php)...


Answered By - charlietfl
Answer Checked By - Katrina (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing