1

I am working on html/javascript and found these imports prone to confusion:

  <link rel="stylesheet" href="../../../../_GUI/css/sample.css">
  <link rel="stylesheet" href="../../../../_GUI/css/button.css">

Multiple ../../ in code doesn't really look nice and looks confusing.

Is there any way I can make something like a variable where in:

GUI = ../../../../_GUI ?

I also checked absolute paths but that would not be so reliable since root folder may change.

Help~

kzaiwo
  • 1,558
  • 1
  • 16
  • 45
  • Did you try with the '~' symbol ? For example: ~/YourFileName/sample.css. This will find your 'sample.css' no matter whatever the case provided the project should be the same. – Rohan Rao Dec 06 '19 at 03:36

1 Answers1

0

you can use a simple declaration like below and use that variable all over the script.

<script type="text/javascript">
 var stylePath = 'http://_GUI/css/sample.css';
</script>

And also you can use jQuery to get the file location in runtime like below.

var fileLocation = $('script[src*=sample]').attr('src'); 
fileLocation = fileLocation.replace('sample.css', '');  

Go to this Question to learn more about this.