0

Here is situation

in Dev Environemt , I have to add css and js file like below

<script src="../../js/file.js">
<link rel="stylesheet" type="text/css" href="../../css/file.css">

in Production Environment , I have to add same css and js file like below with the change in src attribute

<script src="js/file.js">
<link rel="stylesheet" type="text/css" href="css/file.css">

Any solution to do it dynamically instead of manually

Thanks

3
  • see stackoverflow.com/questions/4847313/… Commented Jul 30, 2019 at 12:43
  • 2
    it's possible to do client-side, but IMHO it should be handled on server-side instead Commented Jul 30, 2019 at 12:44
  • It needs to be done on Client-Side in my case Commented Jul 30, 2019 at 12:52

2 Answers 2

0

You can achieve that creating the style and script tag on the fly according to an IF.

    var link = document.createElement('link');
    link.setAttribute('rel', 'stylesheet');
    link.setAttribute('type', 'text/css');
    if (production)
        link.setAttribute('href', 'css/my.css');
    else
        link.setAttribute('href', 'css/dev_my.css');
    document.getElementsByTagName('head')[0].appendChild(link);
Sign up to request clarification or add additional context in comments.

Comments

0

You can use PHP for that.

On the import / include, use <?php echo dirname(dirname($_SERVER['PHP_SELF'])); ?>

E.G:

<link rel="stylesheet" type="text/css" href="<?php echo dirname(dirname($_SERVER['PHP_SELF'])); ?>/css/style.css">

dirname($_SERVER['PHP_SELF']) will return the path to the current file's directory.

dirname(dirname($_SERVER['PHP_SELF'])); will return the path before the current file's directory. You can read more about that here, and about $_SERVER['PHP_SELF'] here.

About using PHP, click here.

1 Comment

You can use __dirname instead. Read here and here.

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.