vizskywalker 0 Report post Posted November 3, 2007 I've been working on an include function for javascript. It works just fine in Firefox and IE, but for some reason, it doesn't result in the loading of the scripts for Safari. The code is as follows: function include(url) { // Include Guard var scripts = document.getElementsByTagName("script"); for (var index = 0; index < scripts.length; ++index) { if (scripts.src == url) { return; } } // Inclusion var head = document.getElementsByTagName("head").item(0); var script = head.appendChild(document.createElement("script")); script.type = "text/javascript"; script.src = url;}Any suggestions as to how to improve this script so it works in Safari would be appreciated. I used alerts to check and make sure that Safari wasn't returning prematurely, so I know it is being "included", but the included scripts aren't being loaded.~Viz Share this post Link to post Share on other sites
turbopowerdmaxsteel 0 Report post Posted November 4, 2007 (edited) It works fine for me on all of these: Internet Explorer 7, Firefox, Opera 9.x, Safari 3.x. Here's the code I have used for the HTML file. <body> <table onclick="include('js.js')"> <tr> <th>Click Me</th> </tr> </table> </body> <script type="text/javascript"> function include(url) { // Include Guard var scripts = document.getElementsByTagName("script"); for (var index = 0; index < scripts.length; ++index) { if (scripts.src == url) { return; } } // Inclusion var head = document.getElementsByTagName("head").item(0); var script = head.appendChild(document.createElement("script")); script.type = "text/javascript"; script.src = url; } </script></html> and the to be included Javascript file:- alert('WTF'); Edited December 2, 2016 by OpaQue (see edit history) Share this post Link to post Share on other sites
vizskywalker 0 Report post Posted November 5, 2007 Yeah, that works, but it doesn't seem to work if the include function and the call to include are in a separate javascript file. As opposed to an embedded script tag like the one you have now. Right now I'm using a workaround where I load the javascript via AJAX for Safari, but I don't like that, and I hate it even more because I have to directly include where I want it as it seems to maintain local scope. Unless I'm messing something up.Edit: Also, I don't know if this matters, but I'm using XHTML 1.1 (Strict), so I don't know if Safari treats that as different from 1.0 Transitional, but since the page is served to Safari as text/html (due to issues with DOM manipulation when it is application/xhtml+xml) I don't think that would be an issue.~Viz Share this post Link to post Share on other sites
seant23 0 Report post Posted June 30, 2008 Hey I use to use a similar function like this, I found a better way thou, as this has a major downside, you can't use the included files in the current script block.Check out my post Their is a Javascript Include Function⌠Share this post Link to post Share on other sites
FirefoxRocks 0 Report post Posted July 1, 2008 Could anyone tell me why this function would be useful (the purpose of this) and are there any issues with cross site scripting? Share this post Link to post Share on other sites
TavoxPeru 0 Report post Posted July 4, 2008 Could anyone tell me why this function would be useful (the purpose of this) and are there any issues with cross site scripting?In my opinion this function could be very useful in the event that you need to add some additional javascript functions to only a specific page not to all pages of the website. Generally what i do is to separate the components of every page in modules, and then start loading these when needed, first the header, then the main body, then the footer and so on of any page.In the header i include a general javascript file which all the pages of the website will use but in only two pages i need to include some additional functions to them. The first thing that i do is to include an extra script tag inside these pages, but when i validate both pages, they don't pass the validation process, so, i use this function to add the additional code needed and after that both pages pass the validation process.Best regards, Share this post Link to post Share on other sites
FirefoxRocks 0 Report post Posted July 5, 2008 In my opinion this function could be very useful in the event that you need to add some additional javascript functions to only a specific page not to all pages of the website. Generally what i do is to separate the components of every page in modules, and then start loading these when needed, first the header, then the main body, then the footer and so on of any page.Umm correct me if I'm wrong, but doesn't this code apply JavaScript functions to one page only:<script type="text/javascript"> function one() { // some code here } function two() { // some code here } </script> Share this post Link to post Share on other sites
TavoxPeru 0 Report post Posted July 7, 2008 Umm correct me if I'm wrong, but doesn't this code apply JavaScript functions to one page only: <script type="text/javascript"> function one() { // some code here } function two() { // some code here } </script> You are not wrong, the code apply to one page only, but, what i'm trying to explain is that i want to include only some extra javascript code to only one page not to every page of the website.I put your code inside a file called library.js and i create another file called library1.js with one function only:<script type="text/javascript"> function three() { // some code here } </script> The idea is to include to every page of the webiste the file library.js and then include the file library1.js to only one page that needs this extra code.Best regards, Share this post Link to post Share on other sites