Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Any way to get the url location of a remote script?

Status
Not open for further replies.

Smapty

Programmer
Mar 12, 2005
55
0
0
US
Is there anyway for a remotely called javascript file to use the href location of the file itself? All I've been able to do is get the location of the calling html page, not where the script itself is located. I have a situation where differant clients are all calling the same script but at differant virtual domains. For example...

Client uses.. <script src="
Client uses.. <script src="
The site is where our server is and where the script is located, but I need to decipher the "src" part of the calling script in order to customize the results. Its on a coldfusion server and we've managed to use the same set of scritps for all the clients, so I don't want to now have to create a new javasript file for each client when all I need is to know what "src" they used in order to send them to the right location (the js file is a "return to site" button) Any way of achieving this?
 
Assuming the script filename is known, and it's just the domain and/or path that is different each time, e.g.:

Code:
<script type="text/javascript" src="[URL unfurl="true"]http://domain1/path1/knownFileName.js"></script>[/URL]

...

<script type="text/javascript" src="[URL unfurl="true"]http://domain2/path2/path2/knownFileName.js"></script>[/URL]

then you could use the loader code from the "script.aculo.us" JS effects library which does just this. It loops around all script elements finding the one whose filename matches a known string, then strips the domain & path from it.

I've used a modified version of this in the past to do just this... it works very well.

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Thanks Dan. Could you give a little pointer on where to start with that code? The syntax used there is a bit more advanced than I'm used to. I've messed around with it but can't seem to extract the "src" value of the script tag. All I can get is "undefined" as a value.

I'm assuming you are talking about this code:


Code:
var Scriptaculous = {
  Version: '1.8.1',
  require: function(libraryName) {
    // inserting via DOM fails in Safari 2.0, so brute force approach
    document.write('<script type="text/javascript" src="'+libraryName+'"><\/script>');
  },
  REQUIRED_PROTOTYPE: '1.6.0',
  load: function() {
    function convertVersionString(versionString){
      var r = versionString.split('.');
      return parseInt(r[0])*100000 + parseInt(r[1])*1000 + parseInt(r[2]);
    }
 
    if((typeof Prototype=='undefined') || 
       (typeof Element == 'undefined') || 
       (typeof Element.Methods=='undefined') ||
       (convertVersionString(Prototype.Version) < 
        convertVersionString(Scriptaculous.REQUIRED_PROTOTYPE)))
       throw("script.aculo.us requires the Prototype JavaScript framework >= " +
        Scriptaculous.REQUIRED_PROTOTYPE);
    
    $A(document.getElementsByTagName("script")).findAll( function(s) {
      return (s.src && s.src.match(/scriptaculous\.js(\?.*)?$/))
    }).each( function(s) {
      var path = s.src.replace(/scriptaculous\.js(\?.*)?$/,'');
      var includes = s.src.match(/\?.*load=([a-z,]*)/);
      (includes ? includes[1] : 'builder,effects,dragdrop,controls,slider,sound').split(',').each(
       function(include) { Scriptaculous.require(path+include+'.js') });
    });
  }
}
 
I think I may have gotten it with this tutorial...



Code:
var scripts = document.getElementsByTagName('script');
var index = scripts.length - 1;
var myScript = scripts[index].src;

myScript = myScript.replace(/test\.js(\?.*)?$/,'');

document.write(myScript);
 
Excellent - that seems to do much the same, but the scriptaculous one doesn't need to rely on the order of the script includes.

If you know your source code won't change that much, then yours is just as good.

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top