简体   繁体   中英

How can I construct a jQuery getScript?

I've made a JavaScript code that is way to big for easy copy-paste. Therefore, it would be perfect to use jQuery's getScript to make it smaller.

I'm having a hard time to get it to work. I tought it maybe could be some spesial way to construct the .js. Here is what I got so far trying to figure it out.

javascript: 
(function() {
$.getScript("https://raw.githubusercontent.com/stianteien/forsvarsoversikt/master/test.js"); 
})()

Directed to this page: https://raw.githubusercontent.com/stianteien/forsvarsoversikt/master/test.js With the simple code:

alert("hello");

...my browser is sending an error:

Loading fail for this <script> with this source

How do I get this to work?

GitHub serves .js files as plain text. You can use $.get() or $.ajax() , create a <script> element and set the .textContent of the <script> element to the response, then append the <script> element to the document see Displaying pdf from arraybuffer

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