1

Using Joomla to create my site. Let say my site is located at

https://domain.com/directory/siteName

I want to get this path in javascript on any page rendered on my site. The issue is, following internal sub menus and site's different settings, the path comes to be any one like these-

  1. https://domain.com/directory/siteName/anyMenu/index.php

  2. https://domain.com/directory/siteName/index.php

  3. https://domain.com/directory/siteName/index.php?option=com_content&id=9

  4. https://domain.com/directory/siteName/anyMenu

  5. https://domain.com/directory/siteName/anyMenu.html

I have tried some solutions like-

var re = new RegExp(/^.*\//);
alert(re.exec(window.location.href));

It truncates the last node as index.php in case 1, also

var dummy = new Image;
dummy.src = '$';                 
alert(dummy.src.slice(0,-1));

does the same thing. So is there any way to get exact address where site is located using javascript only? Also the result must be generic for any site as if it is applied in any extension then that extension can be installed in any site with any level of directory structure.

I require this as I am using a js file which is internally registering another js file. So path for this, to be registered js has to be given in first js

1 Answer 1

2

You can use Joomla's JURI::base() function for that. Remember it's php. so you could do something like:

$document = JFactory::getDocument();
$document->addScriptDeclaration('var base = \''.JURI::base().'\'');

You can now use the base variable in your JS script. Remember to add it to the document after the declaration above.

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

Comments

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.