有没有办法在 location.replace 中添加 document.url

发布于 2024-12-08 13:15:41 字数 451 浏览 1 评论 0原文

这就是我想要实现的目标:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

聚集的泪 2024-12-15 13:15:41
  • 使用encodeURIComponent(location.href),而不是document.URL
    *

    1. 您可以创建一个 IFRame,并设置它的 src 属性。
    2. 另一个选项是执行 XMLHttpRequest [1]
    3. (新图像).src = url
    4. window.open(url)(可能被阻止,妨碍用户)
    5. 创建表单并提交(使用 target=__,其中 __ 可以是 _blank 或框架名称。

[1] 这个方法的作用是 请参阅:MDN:使用 XMLHttpRequest

另 来自服务器的反馈,以 JavaScript 代码的形式)。

javascript:void(function(){
    var s = document.createElement("script");
    s.src = "http://localhost:8888/test/index.php?site="
              + encodeURIComponent(location.href);
    document.body.appendChild(s);
})();

使用图像:

javascript:void(function(){
    (new Image()).src = "http://localhost:8888/test/index.php?site=" +
         encodeURIComponent(location.href);
})();

编辑警报 + 自动关闭

按照评论中的要求,在经过一定时间后自动关闭的纯 JS 警报我保持了功能简单,如果您很懒,JQuery 也是一个选择,尽管包含一个自动隐藏“警报”框的整个框架有点过分了。

/* @name        alertFade
   @description Shows a message, closing automatically after # milliseconds
   @param String message      Message to display
   @param number autoclose_ms Time in milliseconds till the message has to be closed
   @param number softFade_ms  Time in milliseconds for the fade animation*/
function alertFade(message, autoclose_ms, softFade_ms){
    if(typeof message == "undefined") message = "";
    if(isNaN(autoclose_ms)) autoclose_ms = 3000;
    if(isNaN(softFade_ms)) softFade_ms = 1000;
    var container = document.createElement("div"),
        alertBox = document.createElement("div"),
        alertContent = document.createElement("div"),
        dismiss = document.createElement("div");
    container.style.cssText = "position:fixed;top:0;left:0;width:100%;height:100%;display:table;opacity:1;background:transparent;";
    alertBox.style.cssText = "display:table-cell;vertical-align:middle;text-align:center;";
    alertContent.style.cssText = "display:inline-block;white-space:pre-wrap;word-wrap:break-word;background:#DDF;font-size:normal;padding:5px";
    dismiss.style.cssText = "font-size:small;border-bottom:1px solid #CCC";

    dismiss.appendChild(document.createTextNode("(Click to dismiss)"));
    dismiss.onclick = fadeClose;
    alertContent.appendChild(dismiss);
    alertContent.appendChild(document.createTextNode(message));
    alertBox.appendChild(alertContent);
    container.appendChild(alertBox);
    document.body.appendChild(container);
    var alertAutoClose = window.setTimeout(fadeClose, autoclose_ms);
    var fadeTimer;

    function closeAlert(){
        window.clearTimeout(alertAutoClose);
        window.clearInterval(fadeTimer);
        if(container.parentNode) container.parentNode.removeChild(container);
    }
    function fadeClose(){
        if(!softFade_ms) return closeAlert(); //No fade = close immediately

        var opacity = 1;
        fadeTimer = window.setInterval(function(){
            opacity -= .1; //Reduce the opacity by 10% per interval
            if(opacity <= 0) return closeAlert();
            container.style.opacity = opacity;
        }, softFade_ms/10);
    }
}
//Example:
alertFade("Message!");
  • Use encodeURIComponent(location.href), instead of document.URL
    *

    1. You could create an IFRame, and set the src property of it.
    2. Another option is to execute a XMLHttpRequest [1]
    3. (new Image).src = url
    4. window.open(url) (may be blocked, hinders the user)
    5. Create a form, and submit it (using 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).

javascript:void(function(){
    var s = document.createElement("script");
    s.src = "http://localhost:8888/test/index.php?site="
              + encodeURIComponent(location.href);
    document.body.appendChild(s);
})();

Using Image:

javascript:void(function(){
    (new Image()).src = "http://localhost:8888/test/index.php?site=" +
         encodeURIComponent(location.href);
})();

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

/* @name        alertFade
   @description Shows a message, closing automatically after # milliseconds
   @param String message      Message to display
   @param number autoclose_ms Time in milliseconds till the message has to be closed
   @param number softFade_ms  Time in milliseconds for the fade animation*/
function alertFade(message, autoclose_ms, softFade_ms){
    if(typeof message == "undefined") message = "";
    if(isNaN(autoclose_ms)) autoclose_ms = 3000;
    if(isNaN(softFade_ms)) softFade_ms = 1000;
    var container = document.createElement("div"),
        alertBox = document.createElement("div"),
        alertContent = document.createElement("div"),
        dismiss = document.createElement("div");
    container.style.cssText = "position:fixed;top:0;left:0;width:100%;height:100%;display:table;opacity:1;background:transparent;";
    alertBox.style.cssText = "display:table-cell;vertical-align:middle;text-align:center;";
    alertContent.style.cssText = "display:inline-block;white-space:pre-wrap;word-wrap:break-word;background:#DDF;font-size:normal;padding:5px";
    dismiss.style.cssText = "font-size:small;border-bottom:1px solid #CCC";

    dismiss.appendChild(document.createTextNode("(Click to dismiss)"));
    dismiss.onclick = fadeClose;
    alertContent.appendChild(dismiss);
    alertContent.appendChild(document.createTextNode(message));
    alertBox.appendChild(alertContent);
    container.appendChild(alertBox);
    document.body.appendChild(container);
    var alertAutoClose = window.setTimeout(fadeClose, autoclose_ms);
    var fadeTimer;

    function closeAlert(){
        window.clearTimeout(alertAutoClose);
        window.clearInterval(fadeTimer);
        if(container.parentNode) container.parentNode.removeChild(container);
    }
    function fadeClose(){
        if(!softFade_ms) return closeAlert(); //No fade = close immediately

        var opacity = 1;
        fadeTimer = window.setInterval(function(){
            opacity -= .1; //Reduce the opacity by 10% per interval
            if(opacity <= 0) return closeAlert();
            container.style.opacity = opacity;
        }, softFade_ms/10);
    }
}
//Example:
alertFade("Message!");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文