www.webdeveloper.com
Results 1 to 8 of 8

Thread: <script> with source as a local file?

  1. #1
    Join Date
    May 2009
    Posts
    3

    <script> with source as a local file?

    Just a sanity check.

    Can the src URL for the <script> tag be a local file ala:

    <script src="file:///c:/myScript.js">



    I'm making an IE BHO to insert a script into webpages.
    I've got it working for scripts with regular remote URL's (http://etc),
    but not for a local files, which I need.

  2. #2
    Join Date
    May 2006
    Location
    Odenton, MD
    Posts
    1,449
    i don't think is possible because file is on different zone. tried something and at least fx seem to load the file but wouldn't execute the content.

  3. #3
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    it's very possible, in all browsers by default in fact.

    script tags are domain/mimetype agnostic.


    you probably have the path wrong. IE and FF use different path formats.
    You might need two script tags. the extra broken one shouldn't hurt anything.
    try browsing to the script file in IE or firefox, and copying the URL from the addressbar.
    that way, you know you have the exact right formatting...

  4. #4
    Join Date
    May 2009
    Posts
    3
    It's got to be a problem with local vs remote; I've tried every path syntax variation I can think of, capitals, backslashes, /// vs //

    Putting the local file URL in IE's address bar opens the javascript.js file fine.

    Using that local URL as the src attribute and the script doesn't run injected, whereas the remote URL does work injected.

  5. #5
    Join Date
    Jun 2007
    Posts
    667
    Quote Originally Posted by HappyFunBall View Post
    Just a sanity check.

    Can the src URL for the <script> tag be a local file ala:

    <script src="file:///c:/myScript.js">

    Always use a relative path. If your html file is in folder c:/html and your .js file is on the root as you specify, use:

    Code:
    <script src="../myScript.js">

  6. #6
    Join Date
    May 2009
    Posts
    3
    Always use a relative path.
    The HTML can be from anywhere they're browsing.
    Only the injected script is from a fixed local URL.

    This is for a BHO like greasemonkey/IE7Pro, to allow tweaks of all pages browsed.

  7. #7
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    Quote Originally Posted by HappyFunBall View Post
    It's got to be a problem with local vs remote; I've tried every path syntax variation I can think of, capitals, backslashes, /// vs //
    i did notice IE has implemented a notification bar action for this, and i was kinda wrong to say it works by default in all browsers. but, clicking the yellow bar and choosing allow did produce the expected results, so i guess i can still say it works in all browsers without configuration changes...

    note that noscript, the popular firefox extension, blocks local files as a setting, so don't forget to change that if you need to.

    IE8 sees them both, so if you don't need old IE, just the real one works.
    all other browsers i tested only see the one.

    anyways, tested IE8, FF3, S3w, O9.5:

    Code:
    <html>
      <script src="file:///C:/test.js">FF3</script>
      <script src="c:\test.js">IE8</script>
    </html>

    test.js:
    Code:
    alert(555);
    also, don't forget that some OSs/configurations will not allow access to all folders unless the browsers is being run as an administrator and/or has user perms to the specified folder.
    Last edited by rnd me; 05-12-2009 at 02:03 AM.

  8. #8
    Join Date
    Oct 2011
    Posts
    1
    rnd me: In explorer your example works only in the case where the two files are on the same drive. Try moving the html-file test.htm to a share - open it in explorer 8 - notice that no alert box pops up. The version for firefox works fine also in that case. Anybody know a way of referring to a script on a nother drive in explorer?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles