简体   繁体   中英

How to pass value from on javascript to another javascript file when I pass value in function?

<a href="#" onclick="passid(idvalue)" class="generate">Generate ID</a>

Above is my php code from where I pass id value in a JavaScript Function

function passid(id)
{
var gener = "<script type=\"text/javascript\">
             var id = "+id+"<\/script>
             <script src=\"http://mySample.com/MYjs/myJS.js\"><\/script>"; 
document.getElementById('codegen').textContent = gener ; 
}

from this I want to pass ID in myJS.js File How to send this ID.

Why not use Jquery?

$('.generate').on('click', function () {
passid(1);
});

function passid(id) {
    var gener = "<script type=\"text/javascript\">var id = "+id+";alert(id);<\/script>"+"<script>alert(id);<\/script>"+"<script src=\"alert.js\"><\/script>";//Alert inside JS file too!
    $(gener).appendTo(document.body);
}

http://jsfiddle.net/LeroyRon/bk9fryuj/

///No jQuery version Look around to here: Adding <script> element to the DOM and have the javascript run?

Just make id global variable and to change html you need to use innerHTML:

function passid(id) {
    window.id = id;
    var gener = "<script src=\"http://mySample.com/MYjs/myJS.js\"><\/script>"; 
    document.getElementById('codegen').innerHTML = gener; 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM