Javascript小书签可从一页获取信息,然后将其提交到另一页上的表单中 [英] Javascript bookmarklet to take info from one page and submit it to form on another page

查看:59
本文介绍了Javascript小书签可从一页获取信息,然后将其提交到另一页上的表单中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,我在这里发现无法在一页内编写JavaScript来在另一外部页上输入表单数据,我想使用基于浏览器的书签来实现.

Now that I discovered here that I can't write JavaScript within one page to enter form data on another external page, I'd like to do this with a browser-based bookmarklet instead.

我可以使用以下书签代码段访问原始页面上的数据:

I'm able to access the data on my original page with this bookmarklet code snippet:

javascript:var%20thecode=document.myForm.myTextArea.value;

如果我在浏览器中手动打开基于外部Web的表单,此代码将更改文本框中的内容:

If I open the external Web-based form manually in the browser, this code changes what's in the text box:

javascript:void(document.externalForm.externalTextArea.value="HELLO WORLD"));

此小书签代码将使用外部格式打开一个新的浏览器窗口:

And this bookmarklet code will open a new browser window with the external form:

javascript:newWindow=window.open("http://www.url.com","newWindow");if(window.focus){void(newWindow.focus());}

但是,当我尝试将这些代码片段放到一个小书签中以在新窗口中打开外部表单并更改其中的数据时,我无法访问newWindow中的任何元素.例如,这不适用于在新窗口中检查文本区域的现有值

However, when I try to put these snippets together in a single bookmarklet to open the external form in a new window and change the data inside, I can't access any of the elements in newWindow. For example, this doesn't work to check the existing value of the text area in the new window

javascript:var%20newWindow=window.open("http://www.url.com","newWindow");if(window.focus){void(newWindow.focus());}window.alert(newWindow.document.externalForm.externalTextArea.value);

一旦我使用小书签代码以newWindow的形式打开新窗口,似乎无法访问该新窗口中的元素.有什么建议我想念的吗?谢谢.

Once I use the bookmarklet code to open the new window as newWindow, I don't seem to be able to access the elements within that new window. Any suggestions what I'm missing? Thanks.

推荐答案

这是因为小书签在当前网页的沙箱(环境)中运行.由于不允许访问(页面的DOM)没有相同协议,域名和端口的另一个页面,因此,当协议,域和端口不匹配.顺便说一句,访问页面上的iframe也是一样.

That's because the bookmarklet runs within the sandbox (the environment) of the current web page. Since you're not allowed to access (the DOM of) another page which doesn't have the same protocol, domain name and port, you're not able to access the document property of newWindow when protocols, domains and ports don't match. BTW, the same is true for accessing iframes on a page.

在您谈论外部形式"时,我想您不会停留在同一个域中.其他示例(此时)将检索或处理当前页面上的数据,并且不会出错.

As you're talking about an "external form", I guess you don't stay on the same domain. The other examples retrieve or manipulate data on the current page (at that moment) and won't error out.

另请参见同一出处政策.

更新:关于Delicious(等)书签:其代码实际上为:

Update: About the Delicious (et al.) bookmarklet: its code actually reads:

(function () {
    f = 'http://delicious.com/save?url=' + encodeURIComponent(window.location.href) + '&title=' + encodeURIComponent(document.title) + '&v=5&';
    a = function () {
        if (!window.open(f + 'noui=1&jump=doclose', 'deliciousuiv5', 'location=yes,links=no,scrollbars=no,toolbar=no,width=550,height=550'))
            location.href = f + 'jump=yes'
    };
    if (/Firefox/.test(navigator.userAgent)) {
        setTimeout(a, 0)
    } else {
        a()
    }
})()

所以,是的,仅使用GET请求传输参数.

So, yes, the parameters are only transferred using a GET request.

这篇关于Javascript小书签可从一页获取信息,然后将其提交到另一页上的表单中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆