|
|
|
|
![]() ![]() |
Nov 3 2007, 09:30 PM
Post
#1
|
|
|
Techno-Necromancer Group: Members Posts: 1,018 Joined: 13-January 05 From: The Net Member No.: 2,127 |
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:
CODE function include(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.// 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; } ~Viz |
|
|
|
Nov 4 2007, 09:48 AM
Post
#2
|
|
|
Premium Member Group: [HOSTED] Posts: 391 Joined: 16-February 06 From: Kolkata, India Member No.: 11,322 |
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. CODE <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> </head> <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:- CODE alert('WTF');
This post has been edited by turbopowerdmaxsteel: Nov 4 2007, 09:49 AM |
|
|
|
Nov 5 2007, 01:40 AM
Post
#3
|
|
|
Techno-Necromancer Group: Members Posts: 1,018 Joined: 13-January 05 From: The Net Member No.: 2,127 |
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 |
|
|
|
Jun 30 2008, 04:34 PM
Post
#4
|
|
|
Newbie [ Level 1 ] Group: Members Posts: 1 Joined: 30-June 08 Member No.: 31,228 |
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… |
|
|
|
Jul 1 2008, 05:06 PM
Post
#5
|
|
|
Super Member Group: [HOSTED] Posts: 693 Joined: 12-July 06 From: Ontario, Canada Member No.: 14,464 |
Could anyone tell me why this function would be useful (the purpose of this) and are there any issues with cross site scripting?
|
|
|
|
Jul 4 2008, 09:03 PM
Post
#6
|
|
|
Super Member Group: [HOSTED] Posts: 760 Joined: 8-April 06 From: Lima - Peru Member No.: 12,579 |
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, |
|
|
|
Jul 5 2008, 01:54 AM
Post
#7
|
|
|
Super Member Group: [HOSTED] Posts: 693 Joined: 12-July 06 From: Ontario, Canada Member No.: 14,464 |
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: HTML <script type="text/javascript">
function one() { // some code here } function two() { // some code here } </script> |
|
|
|
Jul 7 2008, 11:03 AM
Post
#8
|
|
|
Super Member Group: [HOSTED] Posts: 760 Joined: 8-April 06 From: Lima - Peru Member No.: 12,579 |
Umm correct me if I'm wrong, but doesn't this code apply JavaScript functions to one page only: HTML <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: HTML <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, |
|
|
|
![]() ![]() |
Similar Topics
|
Lo-Fi Version | Time is now: 7th October 2008 - 07:30 AM |