English 中文(简体)
Javascript security / cross scripting on same server
原标题:

Have some Javascript that I need to work via the following:

://localhost

://servername

:/www.domainnamefortheserver.com

When I run the script from http://servername with an IFRAME referencing the domain - it does not load.

Is there a way to get the Javascript security model to recognize the server name, localhost and the domain as the same "domain"?

Thanks

问题回答

If you are running on UNIX you can edit /etc/hosts to give a fake DNS entry for your server.

eg.

127.0.0.1 localhost www.domainnamefortheserver.com

Then you can always connect to it as the correct name even when it s not on the live site yet. Don t try and break the javascript security directly.

This will also work on OSX. Windows works differently, I expect.

If you are using a server-side language to generate the page, you may be able to set the security domain like so:

document.domain = $CURRENT_HOSTNAME;

So the security domain will be the domain the user requested. This is a shot in the dark, but I hope it helps nonetheless.

Use root relative URIs:

href="/foo/bar"

rather than absolute URIs:

href="http://example.com/foo/bar"

That way the document will be loaded from the same hostname.

What do you mean by

my references are to the domain name

?

If you load scripts in your page on http://servername (using <script src= >), they will have access to everything on http://servername, even if they come from another domain.

However, if you try to make AJAX calls to the other domain, then you have a problem. You can use the trick explained by Christopher, ie making aliases to the domain.





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签