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?

4
  • 3
    This question should ideally asked on stackoverflow, imho.
    – Nagarjun
    Jun 10, 2015 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, 2015 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. Jun 11, 2015 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. Jun 11, 2015 at 8:40

3 Answers 3

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
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');
1
  • 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. Jun 11, 2015 at 8:52
-1

Try using the following:

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

Your Answer

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

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