1

I need one help.I need to set js/css path name dynamically uaing Angular.js/Javascript/Jquery.I am explaining my code below.

<script src="/crm/controller/productController.js" type="text/javascript"></script>

Suppose i have the above script tag.Here my path is /crm/controller/productController.js.I need to set /crm/ in my config file and i will set this path name in every script tag.In php it is happening like below.

define("USER_SITE_URL", "http://www.absclasses.com/");
<script type="text/javascript" src="<?php echo SITE_URL;?>js/jquery-1.9.1.min.js"></script>

As i am not using PHP. How it can me made using Angular.js or Javascript/Jquery.

6
  • Possible duplicate of How do I load a javascript file dynamically? Commented Dec 21, 2015 at 5:58
  • @KhalidHussain : meaning is totally different. Commented Dec 21, 2015 at 6:01
  • is that because you have different versions of these files in different folders ? can you give more details about why you want to do that ? Commented Dec 21, 2015 at 6:05
  • I think you don't want to write <?php echo SITE_URL;?> every time so for that you can add <base href="absclasses.com"> tag in side your head tag of page and after it use <script type="text/javascript" src="js/jquery-1.9.1.min.js"></script> Commented Dec 21, 2015 at 6:09
  • @ranakrunal9 : I have already tested that if i am setting the <base href="absclasses.com/"> then i have to add <script type="absclasses.com/text/javascript" src="js/jquery-1.9.1.min.js"></script> like this.that was the reason why i wanted to do it in config file because suppose the someday if the base href value is changed the i have to chahged in everywhere. Commented Dec 21, 2015 at 6:12

3 Answers 3

1

You can use HTML base tag as follows.

<base href="http://www.absclasses.com/" />

If you want to do this with angular config block, you can use the following javascript code also with some modification.

document.write("<base href='http://" + document.location.host + "' />");

You can also define store document.location.host to a variable and use that.

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

4 Comments

Ok,I will check this and confirm you.so how can i set my script tag ?
Same way without the common part, which you are setting on base.
Hi,its good solution but one problem is there,i have activated the html5 mode true and unable to open the link in new tab.Do you have any solution.
I think that is a different problem.
0

In the javascript, you can set global variable.

function globalSource() {
    jqueryUrl = "http://xxxxxxx",
    angularUrl = "http://xxxxxx"
}

when you want to use.

(function() {
  globalSource();
  //Get your dom and change attributes src link
  //ex: var xx = document.getElementsByTagName('script')[0].setAttribute("src", window.jqueryUrl );
})();

3 Comments

@ user3762171 : In this way i have to load all file dynamically.But i need to set only some common part of the path.
Ok,Can i call globalSource inside from script tag?
Yes, you can. If you want to use it everywhere, you have to load this file when document readyState equal "complete". Or your server can dynamically to set.
0
<xsl:variable name="Path" select="/crm/controller/"></xsl:variable> <!-- Global path variable. -->
<xsl:variable name="myScriptPath" select="concat($Path, 'productController.js')"></xsl:variable> <!-- Relative script path variable. -->
<script src="{$myScriptPath}"/> <!-- Attach script. -->

1 Comment

This will not work because HTML can not process your + (concat) operator.

Your Answer

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