有没有办法在 location.replace 中添加 document.url
这就是我想要实现的目标:
location.replace('http://localhost:8888/test/index.php?site=' + document.URL);
所以我需要将当前 URL 作为 site
变量的字符串发送。虽然 document.URL
不起作用,但有没有办法让它起作用?
这个想法是当我转到index.php时,“站点”被插入到数据库中。我希望上面的链接作为书签使用,(问题的第二部分)有没有办法执行该代码但仍保留在同一页面中?
基本上,如果我在 google.com 并单击书签来执行上面的代码,有没有办法保留在 google.com 但让 ..index.php 在后台运行
目前我真的需要帮助问题的第一部分 - 但也许您对如何实现第二部分有任何提示?
This is what I am trying to accomplish:
location.replace('http://localhost:8888/test/index.php?site=' + document.URL);
So I need the current URL to be send as a string of the site
variable. Though the document.URL
is not working, is there a way to get that working?
The idea is when I go to index.php the 'site' is inserted into a database. I would like the above link to work as a bookmarklet, (second part of the question) is there a way to execute that code but to still remain in the same page?
Basically if I am at google.com and click on the bookmarklet to execute the code above, Is there a way to remain at google.com but for the ..index.php to run in the background
At the moment I really need help with the first part of the question - but maybe you have any tips on how to achieve the second part?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
encodeURIComponent(location.href)
,而不是document.URL
*
src
属性。(新图像).src = url
window.open(url)
(可能被阻止,妨碍用户)target=__
,其中__
可以是_blank
或框架名称。[1] 这个方法的作用是 请参阅:MDN:使用 XMLHttpRequest。
另 来自服务器的反馈,以 JavaScript 代码的形式)。
使用图像:
编辑警报 + 自动关闭
按照评论中的要求,在经过一定时间后自动关闭的纯 JS 警报我保持了功能简单,如果您很懒,JQuery 也是一个选择,尽管包含一个自动隐藏“警报”框的整个框架有点过分了。
encodeURIComponent(location.href)
, instead ofdocument.URL
*
src
property of it.(new Image).src = url
window.open(url)
(may be blocked, hinders the user)target=__
, where__
can be_blank
or a frame name.[1] This method does not work across different domains. See also: MDN: Using XMLHttpRequest.
Another method (allows passing feedback from the server, in the form of JavaScript code).
Using Image:
EDIT alert + auto close
As requested in the comments, a pure JS alert which automatically closes after a certain time has elapsed. I've kept the function simple, for educative purposes. If you're lazy, JQuery is also an option, although it's overkill to include a whole framework for automatically hiding an "alert" box