最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - use tags <script> with if and else - Stack Overflow

programmeradmin3浏览0评论

I have some script tag like this:

<script src="cordova-2.5.0.js"></script>
<script src="js/jquery-1.7.2.min.js"></script>
<script src="js/jquery.mobile-1.1.1.min.js"></script>
<script src="js/jquery.xdomainajax.js"></script>
<script src="js/xml2json.js"></script>
<script src="js/ZipPlugin.js"></script>
<script src="js/jquery-ui.min.js"></script>
<script src="js/jquery.ui.touch-punch.min.js"></script>
<script src="js/prefixfree.min.js"></script>

This is my app i wrote using phonegap for android but i want to use code in web. And i dont use all for web page.

Have any way to do it like using if else like this in html:

if(anything) {
    <script src="cordova-2.5.0.js"></script>
    <script src="js/jquery-1.7.2.min.js"></script>
    <script src="js/jquery.mobile-1.1.1.min.js"></script>
    <script src="js/jquery.xdomainajax.js"></script>
    <script src="js/xml2json.js"></script>

    <script src="js/prefixfree.min.js"></script>
} else {
    <script src="js/ZipPlugin.js"></script>
    <script src="js/jquery-ui.min.js"></script>
    <script src="js/jquery.ui.touch-punch.min.js"></script>
}

I am a dumper, please help me. Thanks for reading!!!

Edit:

if i want to change my script tag:

<script src="js/prefixfree.min.js"></script>

Become like this:

<script src=".min.js"></script>

Have anyway to make a variable like:

var key ="/"

And then use in tag like this:

<script src = "key + 'js/prefixfree.min.js'"></script> 

I have some script tag like this:

<script src="cordova-2.5.0.js"></script>
<script src="js/jquery-1.7.2.min.js"></script>
<script src="js/jquery.mobile-1.1.1.min.js"></script>
<script src="js/jquery.xdomainajax.js"></script>
<script src="js/xml2json.js"></script>
<script src="js/ZipPlugin.js"></script>
<script src="js/jquery-ui.min.js"></script>
<script src="js/jquery.ui.touch-punch.min.js"></script>
<script src="js/prefixfree.min.js"></script>

This is my app i wrote using phonegap for android but i want to use code in web. And i dont use all for web page.

Have any way to do it like using if else like this in html:

if(anything) {
    <script src="cordova-2.5.0.js"></script>
    <script src="js/jquery-1.7.2.min.js"></script>
    <script src="js/jquery.mobile-1.1.1.min.js"></script>
    <script src="js/jquery.xdomainajax.js"></script>
    <script src="js/xml2json.js"></script>

    <script src="js/prefixfree.min.js"></script>
} else {
    <script src="js/ZipPlugin.js"></script>
    <script src="js/jquery-ui.min.js"></script>
    <script src="js/jquery.ui.touch-punch.min.js"></script>
}

I am a dumper, please help me. Thanks for reading!!!

Edit:

if i want to change my script tag:

<script src="js/prefixfree.min.js"></script>

Become like this:

<script src="http://smartphone.thnt.vn/VietGames/GhepTranhTu/Web/js/prefixfree.min.js"></script>

Have anyway to make a variable like:

var key ="http://smartphone.thnt.vn/VietGames/GhepTranhTu/Web/"

And then use in tag like this:

<script src = "key + 'js/prefixfree.min.js'"></script> 
Share Improve this question edited May 5, 2013 at 9:01 Sachin Jain 21.8k34 gold badges110 silver badges176 bronze badges asked May 4, 2013 at 8:45 Lê HuyLê Huy 1951 gold badge4 silver badges13 bronze badges 2
  • 5 Not sure what "I am a dumper" means, but I'm thrilled you let us know. – Jordan Arsenault Commented May 4, 2013 at 9:06
  • I must be missing something here! If you are creating a phonegap application, then just included the files you want such as cordova js when you "compile" the app. if you want to deploy the code to a web page, simply remove the references to phonegap such as cordova when you deploy to a web server! – TheTechy Commented May 4, 2013 at 9:11
Add a comment  | 

7 Answers 7

Reset to default 5

How about this,

function registerScript(scriptPath) {
    var scriptTag = document.createElement('script');
    scriptTag.type = 'text/javascript';
    scriptTag.async = true;
    scriptTag.src = scriptPath;
    var s = document.getElementsByTagName('script')[0];
    s.parentNode.insertBefore(scriptTag, s);
}

if (anything) {
    registerScript("cordova-2.5.0.js");
}

For the first part. you can do this:

if (anything) {
    $('head').append('<script src="cordova-2.5.0.js"></script>')
        .append('<script src="js/jquery-1.7.2.min.js"></script>')
        .append('<script src="js/jquery.mobile-1.1.1.min.js"></script>')
        .append('<script src="js/jquery.xdomainajax.js"></script>')
        .append('<script src="js/xml2json.js"></script>')    
        .append('<script src="js/prefixfree.min.js"></script>')
} else {
    $('head').append('<script src="js/ZipPlugin.js"></script>')
        .append('<script src="js/jquery-ui.min.js"></script>')
        .append('<script src="js/jquery.ui.touch-punch.min.js"></script>')
}

To change the script tag, do this:

var key ="http://smartphone.thnt.vn/VietGames/GhepTranhTu/Web/"
$('script[src="js/prefixfree.min.js"]').attr('src', key + 'js/prefixfree.min.js');

Using jquery template :

DEMO : http://jsfiddle.net/abdennour/R36Qc/3/

Your template :

<script id="myscripts" type="text/x-jquery-tmpl">
   <script src="${myurl}" type="text/javascript">
    {{html "</sc"+"ript>"}}
</script>

Your javascript code :

 $('#myscripts').tmpl(myarray).prependTo('head')

The branching (if) is done on array of String (src of js) :

var myarray=[]
if (anything) {
   myarray=[{myurl:"cordova-2.5.0.js"},
{myurl:"js/jquery-1.7.2.min.js"},
{myurl:"js/jquery.mobile-1.1.1.min.js"},
  {myurl:"js/jquery.xdomainajax.js"},
 {myurl:"js/xml2json.js"},
  {myurl:"js/prefixfree.min.js"},
  {myurl:"js/jquery.mobile-1.1.1.min.js"}

 ]

} else {
     myarray=[{myurl:"js/ZipPlugin.js"},
              {myurl:"js/jquery-ui.min.js"},
              {myurl:"js/jquery.ui.touch-punch.min.js"}  ]

}

See : https://stackoverflow.com/a/5462679/747579

To check result , open browser inspector :

You could make a script to create all the scripts node like:

<script>
var head= document.getElementsByTagName('head')[0];
var key = '';
if(anything) key = 'http://smartphone.thnt.vn/VietGames/GhepTranhTu/Web/';
var scripts = new Array()
scripts[0] = 'js/prefixfree.min.js';
scripts[1] = 'js/foo.js';
for(var s in scripts) {
   var script= document.createElement('script');
   script.type= 'text/javascript';
   script.src= key + s;
   head.appendChild(script);
}
</script>

You could use pure javascript like that:

<script>
   var key ="http://smartphone.thnt.vn/VietGames/GhepTranhTu/Web/",
       file=document.createElement('script');
   file.type = "text/javascript";
   file.src = key + 'js/prefixfree.min.js';
   document.getElementsByTagName("head")[0].appendChild(file);
</script>
<script>
if(foo) {
    document.write('<script src="a.js"><\/script>');
} else {
    document.write('<script src="b.js"><\/script>');
}
</script>

In this case document.write is a valid option. The script tags are blocking anyway.

You can include script files dynamically using JavaScript. See an example here: Load jQuery with Javascript and use jQuery

发布评论

评论列表(0)

  1. 暂无评论