Welcome Guest ( Log In | Register )



 
Reply to this topicStart new topic
> Include Function For Javascript
vizskywalker
post Nov 3 2007, 09:30 PM
Post #1


Techno-Necromancer
Group Icon

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) {
  // 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
Go to the top of the page
 
+Quote Post
turbopowerdmaxst...
post Nov 4 2007, 09:48 AM
Post #2


Premium Member
Group Icon

Group: [HOSTED]
Posts: 392
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
Go to the top of the page
 
+Quote Post
vizskywalker
post Nov 5 2007, 01:40 AM
Post #3


Techno-Necromancer
Group Icon

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
Go to the top of the page
 
+Quote Post
seant23
post Jun 30 2008, 04:34 PM
Post #4


Newbie [ Level 1 ]
Group Icon

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…
Go to the top of the page
 
+Quote Post
FirefoxRocks
post Jul 1 2008, 05:06 PM
Post #5


Super Member
Group Icon

Group: [HOSTED]
Posts: 702
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?
Go to the top of the page
 
+Quote Post
TavoxPeru
post Jul 4 2008, 09:03 PM
Post #6


Super Member
Group Icon

Group: [HOSTED]
Posts: 763
Joined: 8-April 06
From: Lima - Peru
Member No.: 12,579



QUOTE(FirefoxRocks @ Jul 1 2008, 12:06 PM) *
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,
Go to the top of the page
 
+Quote Post
FirefoxRocks
post Jul 5 2008, 01:54 AM
Post #7


Super Member
Group Icon

Group: [HOSTED]
Posts: 702
Joined: 12-July 06
From: Ontario, Canada
Member No.: 14,464



QUOTE(TavoxPeru @ Jul 4 2008, 04:03 PM) *
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>
Go to the top of the page
 
+Quote Post
TavoxPeru
post Jul 7 2008, 11:03 AM
Post #8


Super Member
Group Icon

Group: [HOSTED]
Posts: 763
Joined: 8-April 06
From: Lima - Peru
Member No.: 12,579



QUOTE(FirefoxRocks @ Jul 4 2008, 08:54 PM) *
Umm correct me if I'm wrong, but doesn't this code apply JavaScript functions to one page only:
HTML
&lt;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
&lt;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,
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Getting Screen Resolution using Javascript.(8)
  2. How To Create A Popup Window With Javascript?(19)
  3. javascript vs java(12)
  4. Javascript: Disable Mouse Right-click In Browser(16)
  5. Best Way To Learn Javascript(9)
  6. Javascript: Text To Texbox And Back To Text(2)
  7. Dynamicdrive: Good Site For JavaScript Codes(5)
  8. Downloads With Javascript?(7)
  9. Javascript: Simple Slidedown Menu(2)
  10. JavaScript: Simple Dropdown Menu(1)
  11. Calling Of Functions Between Mulitple External Javascript Files(2)
  12. Javascript: Browser Detection Script(0)
  13. JavaScript: Hide And Show Any Element With CSS(5)
  14. Ever Needs To Find Out A Table Height Or With With JavaScript(2)
  15. Create And Import JavaScript Modules For A Large Script(2)
  1. Vertical Marquee Using JavaScript(0)
  2. JavaScript Frames & Querystring(4)
  3. JavaScript Off Redirect Script(2)
  4. I Need Help With Javascript.(7)
  5. Problems With Dynamically Loading Javascript(2)
  6. Add Text To Textarea(6)
  7. Javascript Question(4)
  8. Javascript: How Do I Create Embedded Pop-up Windows?(7)
  9. Problem With Javascript Alert();(9)
  10. Fun With Javascript And Forms(2)
  11. Copy To Clipboard Function(5)
  12. Javascript Help Needed : Alert(z) Works Fine But Document.write Not(2)
  13. Javascript Changes Aren't Working.(6)


 



- Lo-Fi Version Time is now: 14th October 2008 - 03:32 AM