2016-05-30 4 views
0

以下のコードは、ページロード時に常にスクリプトsrcを読み込みます。オンクリックでの外部スクリプトソースの読み込み

私は、クリックが右側(メニュー項目として)にあり、次に右側のページ(コンテンツ)をロードしますが、1ページのみをロードするウェブサイトを持っています。

onclickでのみ外部スクリプトsrcをロードする方法はありますか?

<html> 

<head> 
<script src="https://xdomain.com/xout.js"></script> 
</head> 


<body> 
<a href="#xrefer"> <img id="idxclick" src="images/ximage.jpg" /></a> 

<script> 
var handler = xout.xsetup({ 
    key: 'sd_FlP9fKY7TvINq4bWachJQ35P', 
    image: '/images/xout.png', 
    locale: 'auto', 
    token: function(token) { 
    // 
    // 
    } 
}); 

$('#idxclick').on('click', function(e) { 
    handler.open({ 
    name: 'Hardware', 
    description: 'Software', 
    amount: '50.00' 
    }); 
    e.preventDefault(); 
}); 

$(window).on('popstate', function() { 
    handler.close(); 
}); 
</script> 

</body> 
</html> 

私はここで何をあなたのコード>でした>が、同じことが>

<html> 

<head> 
<!-- script src="https://xdomain.com/xout.js"></script> REMOVED --> 
</head> 


<body> 
<a href="#xrefer"> <img id="idxclick" src="images/ximage.jpg" /></a> 

<script> 
$.getScript('https://xdomain.com/xout.js', function() { 
// do some stuff after script is loaded 

var handler = xout.xsetup({ 
    key: 'sd_FlP9fKY7TvINq4bWachJQ35P', 
    image: '/images/xout.png', 
    locale: 'auto', 
    token: function(token) { 
    // 
    // 
    } 
}); 

$('#idxclick').on('click', function(e) { 
    handler.open({ 
    name: 'Hardware', 
    description: 'Software', 
    amount: '50.00' 
    }); 
    e.preventDefault(); 
}); 

$(window).on('popstate', function() { 
    handler.close(); 
}); 

}); 
</script> 

</body> 
</html> 

が起こります。

あなたの答えは正しかった!、ありがとうございました!そしてこれははるかに良くそしてより速いです!ダブルクリックの防止。

<html> 

<head> 
</head> 


<body> 
<a href="#xrefer"> <img id="idxclick" src="images/ximage.jpg" onmouseover="$.getScript('https://xdomain.com/xout.js')" /></a> 

<script> 
    $('#idxclick').on('mouseout', function() {x=0}) 
    var x=0; 
    $('#idxclick').on('click', function(e) { 
    if (x==0) { 
    x=1; 
var handler = xout.xsetup({ 
    key: 'sd_FlP9fKY7TvINq4bWachJQ35P', 
    image: '/images/xout.png', 
    locale: 'auto', 
    token: function(token) { 
    // 
    // 
    } 
}); 


    handler.open({ 
    name: 'Hardware', 
    description: 'Software', 
    amount: '50.00' 
    }); 
    } 
    e.preventDefault(); 
}); 

$(window).on('popstate', function() { 
    handler.close(); 
}); 
</script> 

</body> 
</html> 

答えて

0

あなたは動的にあなたのクリックイベントハンドラの$.getScript()内部でスクリプトを読み込むことができます。

$('#idxclick').on('click', function(e) { 
    $.getScript('https://xdomain.com/xout.js', function() { 
     var handler = xout.xsetup({ 
       key: 'sd_FlP9fKY7TvINq4bWachJQ35P', 
       image: '/images/xout.png', 
       locale: 'auto', 
       token: function(token) { 
        // 
        // 
       }; 
     handler.open({ 
      name: 'Hardware', 
      description: 'Software', 
      amount: '50.00' 
     }); 
    }); 
    e.preventDefault(); 
}); 
+0

私はすでにこれを試してみましたが、うまくいきませんでした。上記のサンプルで全身のページ(サンプルコード)を作ってください。コード内のどこに適切に貼り付けるのですか? – mayzul

+0

私はあなたのコードを適用し、それを見て、同じことが起こる。 – mayzul

+0

**イベントハンドラー**内に配置する必要があります。私は答えを明確にするために完全なコード例を追加しました。 –

関連する問題