javascript - jquery jsonp principle: js tag dynamically generated position problem
迷茫
迷茫 2017-05-19 10:33:18
0
4
748
The principle of

jquery jsonp is to use the src attribute in the script tag to solve the cross-domain problems encountered in front-end and back-end data requests. One thing I don’t understand is that jquery dynamically generates The script tag is appended to the head tag, which is the head of the document; generally our callback function is in body, the function is declared first and then used, without declaration Why can we get the data passed from the background even if we call it directly?

The following is a test I did:

//同源策略下有两个文件:a.html和b.js.
//a.html中的内容为:


//<script type="text/javascript" src="b.js"></script>

function test(val){
    console.log(val)
}

//<script type="text/javascript" src="b.js"></script>


//b.js的内容为:
test(10)

If this string of code is placed below the declared test function, the number 10 will be printed. If it is placed above the test function, an error will be reported

ReferenceError: test is not defined

Comparing the implementation of jquery, I don’t quite understand why the dynamically generated js will be executed if it is called before the declared function?

迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

reply all(1)
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!