0

I have this code to prevent executing on page ucp.php?mode=register, but i want it to work on other pages. With this code its not working (not executing on all webpages).:

<script>
if ( window.location.toString().match(/[/ucp.php\?mode=register]/)){ 
    var fileref=document.createElement('script')
    fileref.setAttribute("type","text/javascript")
    fileref.setAttribute("src","//sk.search.etargetnet.com/generic/uni.php?g=5:500")
    fileref.setAttribute("async","true")
    fileref.setAttribute("data-ad-type","iframe v2.0")
    fileref.setAttribute("charset","utf-8")

 }
</script>

How to prevent exectugin this JS:

    <script type="text/javascript" async="true" data-ad-type="iframe v2.0" charset="utf-8" src="//sk.search.etargetnet.com/generic/uni.php?g=5:500"></script>

on page ucp.php?mode=register

Thats the question.

6
  • window.location.toString().match(/[/ucp.php\?mode=register]/) is matching the current page URL and looking for a pattern of [/ucp.php\?mode=register]. You'll need to alter this pattern, or simply remove this condition, and only place this code on pages where you want it to be executed. If you'd like to alter the pattern, do some research on RegEx. Commented Sep 24, 2018 at 13:38
  • I have robust webpage so I dont wanna put the code on each folder. Just on the page ucp.php?mode=register I want to not execute the script above. Commented Sep 24, 2018 at 13:44
  • Why not just use normal operations on window.location instead of regex? window.location.pathname will give you the page, window.location.search will give you the query string. Commented Sep 24, 2018 at 13:57
  • How to do it with window.location.pathname? Could you please send answer? Commented Sep 24, 2018 at 13:58
  • @MarcelinoPietero is that any reason why you dont what to do this in php? Commented Sep 24, 2018 at 14:01

4 Answers 4

3

On the first line of your script you check which url it matches if ( window.location.toString().match(/[/ucp.php\?mode=register]/)){

you need to change this to include all URLs you want the script to work on. Since we don't know which pages you want it to know on, we can't tell you exactly what to put there.

If you want it to work on all pages, just remove the if ... and the } that closes the if

Sign up to request clarification or add additional context in comments.

1 Comment

I have robust webpage so I dont wanna put the code on each folder. Just on the page ucp.php?mode=register I want to not execute the script above.
1

If you want it to execute on all page except the one you are searching for, you need to first negate the match, since you are searching for that specific page and you want the OPPOSITE of that.

Second, your regex is broken. You need to escape the several characters, since they act as control characters and mean very specific things to the regex parser.

<script>
if ( !window.location.toString().match(/\/ucp\.php\?mode=register/g)){ 
    var fileref=document.createElement('script')
    fileref.setAttribute("type","text/javascript")
    fileref.setAttribute("src","//sk.search.etargetnet.com/generic/uni.php?g=5:500")
    fileref.setAttribute("async","true")
    fileref.setAttribute("data-ad-type","iframe v2.0")
    fileref.setAttribute("charset","utf-8")
    document.getElementsByTagName('head')[0].appendChild(fileref);
 }
</script>

Which is why people joke about solving a problem with regex now gives you two problems.

7 Comments

Now not executing the JS script on all pages. I want to not execute on the just specific page ucp.php?mode=register -> phpBB register form.
Then just remove the ! in front of window
Still not works. This is the code i want to not execute on that page: <script type="text/javascript" async="true" data-ad-type="iframe v2.0" charset="utf-8" src="//sk.search.etargetnet.com/generic/uni.php?g=5:500"></script>
I mistook your escaped ? for \? I edited my code. Try again.
@HashimAziz Not that I recall. It will simply match all occurrences.
|
0

Try this, handle the logic in php rather than in JavaScript

<?php if(isset($_GET['mode']) && isset($_GET['mode']!="register")) {
<script>

    var fileref=document.createElement('script')
    fileref.setAttribute("type","text/javascript")
    fileref.setAttribute("src","//sk.search.etargetnet.com/generic/uni.php?g=5:500")
    fileref.setAttribute("async","true")
    fileref.setAttribute("data-ad-type","iframe v2.0")
    fileref.setAttribute("charset","utf-8")

 }
</script>
<?php } ?>

2 Comments

The question is for JavaScript.
I don’t think this is a bad answer. Just because the OP asked for a JS solution doesn’t make the OP right. It could be they’ve just got in too deep and didn’t think to look elsewhere. “My hammer won’t screw in this nail, but I want to use a hammer. Have you tried using a screwdriver?” As it’s a param it’s easy to get to in PHP.
0

Try this instead of yours:

if (/\/ucp\.php\?mode=register$/i.test(window.location.href)==false){ 
     //your codes ....
 }

Also you can add script dynamically to your document.head:

if (/\/ucp\.php\?mode=register$/i.test(window.location.href)==false){ 
     var scr=document.createElement("script");
     scr.src="//sk.search.etargetnet.com/generic/uni.php?g=5:500";
     scr.type="text/javascript";
     document.head.appendChild(scr);
 }

1 Comment

Not working. Not executing script on all sites... I want to not execute just on specific :(

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.