1

I'm using Joomla 3 and want to use Javascript to find the base url of the page, extend this url with some other bits and send a variable through the url. Then open this page in the same window.

Specifically if the base url is 'www.webpage.com', I would like to add '\index.php\animals' and then a variable '?delete=true'.

So far my code looks like this:

var urlRedirect = location.host + '/index.php/animals?delete=true';
window.open(urlRedirect,"_self",false);

If I use an alert to view urlRedirect I can see the entire url I would expect eg www.webpage.com/index.php/animals?delete=true but the page is not opening where I want. It is just going to be the base url, I suspect the default page if a weird url is entered.

What code should I use to do this?

  • 3
    This question should ideally asked on stackoverflow, imho. – Nagarjun Jun 10 '15 at 17:44
  • When you type www.webpage.com/index.php/animals?delete=true into your address bar and then go there, does it do what you expected? – TryHarder Jun 11 '15 at 0:14
  • @Nagarjun, sorry I should have made clear I am using Joomla 3 in case there is a special way of doing this with Joomla. – Hannah Smith Jun 11 '15 at 8:39
  • @moomoochoo, I've just realised there was a typo in my question which I've amended. But yes www.webpage.com/index.php/animals?delete=true takes me to exactly the page I want. – Hannah Smith Jun 11 '15 at 8:40
2

I found the answer. In Joomla I don't need to add the host. The below does what I need.

window.open('animals?delete=true',"_self",false);
0

Use below code to get the host name

window.location.host

And to open the url in same tab/page, use either

window.location.href = window.location.host + '/index.php/animals/?delete=true';

or

window.open(window.location.host + '/index.php/animals/?delete=true','_self');
  • Unfortunately this is still not working for me. The redirect takes me to www.webpage.com/index.php, which I think is the default page it takes you to if it doesn't understand the url. Also note I had a typo in the question, the end bit has been amended to be /animals?delete=true. – Hannah Smith Jun 11 '15 at 8:52
-1

Try using the following:

window.location.href = '/path';
  • 1
    A good answer should contain some kind of explanation. – FFrewin Jun 11 '15 at 10:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.