2

I need to show some text on one page only if URL contain specific word. I tried few examples I found on stackoverflow but nothing seems to work.

So I need to show text on this URL - "/index.php?option=com_registration&task=register"

I tried to write the code to recognise word "register" in the URL, but it doesn't work. The text is visible on all pages.

EDIT: The text I want to show is "Creating account will automatically put you on our bi-monthly newsletter email list. Please note: you can unsubscribe at any time."

And it should be located before every content on page.

VderKaiser
  • 127
  • 1
  • 1
  • 9
  • What do you mean by "the text is visible on all pages"? – Dumisani Jul 26 '18 at 08:33
  • @Dumisani I tried few examples, and the text is always visible on every page. And I need it to be visible only on page where URL contain word "register" – VderKaiser Jul 26 '18 at 08:35

3 Answers3

3

You may parse the browser's location URL using the URL API and then search the task parameter's value. If it matches register, perform the logic to show the text.

var url = new URL(window.location.href);

if (url.searchParams.task === 'register') {
  // perform logic to show the text
}

Note that URL API is not available in Internet Explorer. You need to parse the URL params manually in this case, or use a third-party library, such as URI.js.

31piy
  • 23,323
  • 6
  • 47
  • 67
2

your URL is - "/index.php?option=com_registration&task=register" if you are doing it with php

<?php 
  $check = $_GET['task'];
  if(isset($check)){
     #do something...
  }
?>

Point is to take query from url and check if it is set, you are doing it with GET method this way, so you will take it with $_GET['parameterName'], variable $check will have value of parameter assigned value

Armin
  • 373
  • 2
  • 13
0

I really like @31piy's answer, though if you have to support Internet Explorer and don't want to add a whole library just for simple URL matching, you could always use good old Regular Expressions or even indexOf.

For you example you could just use a RegEx for "register" or "task=register".

If you want to go for the simple comparison (e.g. contains task=register) then I would choose the string comparison:

if (window.location.href.indexOf('task=register') !== -1) {
    // show text here
}

If you want to be more flexible regarding the matching you should use a RegEx:

var regex = /task=register/;

if (regex.test(window.location.href)) {
    // show text here
}

For more information on Regular Expressions I highly recommend using an explanatory tool such as this one.

FatalMerlin
  • 1,463
  • 2
  • 14
  • 25
  • This should be done on server side, not client, one of reasons is support from browsers – Armin Jul 26 '18 at 08:40