Passing Querystring style parameters into Javascript file

后端 未结 2 2226
感动是毒
感动是毒 2020-12-21 15:39

Not sure if this is possible or even if I should do it, but I think it\'s quite interesting.

I have a javascript file which I\'m referencing in a flat HTML page. I\

2条回答
  •  执念已碎
    2020-12-21 16:10

    I don't think that passing in variables via the src attribute is possible out of the box without some extra coding on your part (there is an article here if you are interested!). You could do the following though, which should provide the same functionality as you are looking for:

    Define your "config" variables in a single script block on your HTML page:

    
    

    Reference your external JS file in a second script block:

    
    

    Add this code to your external JS file to reference the "local" variable in your HTML:

    var conf1 = window.config1;
    
    if (conf1) {
      // Do stuff
    }
    

提交回复
热议问题