简体   繁体   English

将更改的网址保存为JavaScript变量?

[英]Saving a url that changes as a javascript variable?

This is my first post so I'm sorry if I make any mistakes, I am trying my hardest. 这是我的第一篇文章,对不起,如果我有任何错误,我会尽力而为。 I have been looking for a couple of days now and I can't find a solution, I don't even know if it is possible. 我一直在寻找几天,但找不到解决方案,我什至不知道是否可能。 What I am trying to do is basic, I need to save a URL that changes as a JavaScript variable to call upon later on in a website. 我想做的是基本的,我需要将一个更改为URL的URL保存为JavaScript变量,以供日后在网站中调用。 The URL is http://en.m.wikipedia.org/wiki/Special:Random and it loads a random page. 该URL是http://en.m.wikipedia.org/wiki/Special:Random ,它会加载一个随机页面。 I want the variable to be the URL of the random page yet it is always just "http:// en.m.wikipedia.org/wiki/Special:Random". 我希望变量是随机页面的URL,但始终始终是“ http:// en.m.wikipedia.org/wiki/Special:Random”。 I thought I might need to load it somewhere else first for example in an iframe and then call the URL from there but it just stays as "http:// en.m.wikipedia.org/wiki/Special:Random". 我以为我可能需要先将其加载到其他地方(例如,在iframe中),然后再从那里调用URL,但它始终保持为“ http:// en.m.wikipedia.org/wiki/Special:Random”。 Any help would be greatly appreciated. 任何帮助将不胜感激。

The page you linked redirects you when you access it. 您链接的页面在您访问时会重定向您。 That means you have to request it before you get the actual random page you want. 这意味着您必须先请求它,然后才能获得所需的实际随机页面。

One way you could try to do it is to load the random page in an iframe and check out the iframe's location.: 您可以尝试执行的一种方法是将随机页面加载到iframe中,并检查iframe的位置。:

var iframe = document.createElement('iframe');
iframe.src = 'http://en.m.wikipedia.org/wiki/Special:Random';
iframe.name = "random";
document.body.appendChild(iframe);
console.log(window.frames['random'].location.href);

If you try that you will get a nasty sounding error, like this: 如果尝试这样做,将收到令人讨厌的听起来错误,如下所示:

Uncaught SecurityError: Blocked a frame with origin " http://run.jsbin.io " from accessing a frame with origin " http://en.m.wikipedia.org ". 未捕获到的SecurityError:阻止了源为“ http://run.jsbin.io ”的框架访问源为“ http://en.m.wikipedia.org ”的框架。 Protocols, domains, and ports must match. 协议,域和端口必须匹配。

The reason for it is because of the Same Origin policy which says that location is sensitive information and you're only allowed to access it if the iframe has the same protocol, domain and port as the page which is trying to access it. 这样做的原因是因为“同源”策略说位置是敏感信息,只有在iframe与尝试访问它的页面具有相同的协议,域和端口的情况下,您才可以访问它。

The only solution now is to have a page on the server that makes a request to the random page, gets a response and grabs the url of that response. 现在唯一的解决方案是在服务器上有一个页面,该页面向随机页面发出请求,获取响应并获取该响应的url。 You can retrieve the url either through AJAX or you can simply integrate it in the page server side: 您可以通过AJAX检索url,也可以将其集成到页面服务器端:

<html>
<body> This is my page and this is the random url:
<?php
  ... curl request to http://en.m.wikipedia.org/wiki/Special:Random from which you get the url
  echo $url;
?>
</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM