Change text box value of an iframe on a different domain using jQuery?

我怕爱的太早我们不能终老 提交于 2020-01-02 12:21:11

问题


I have an Iframe for a page that allows people to sign up for my email newsletters.

I want to auto fill the email field in the iframe based on the querystring url (page.html?email=email)

I know how to do the querystring stuff, I'm just not sure If I'm able to access the input text box within the frame. The form or the textbox neither have id's they only have names.

I've tried the following..

$("email_frame").contents.find('input[name=email]').append("test");

Any help would be great.


回答1:


If an IFRAME is loaded from a different domain than the main page, the 2 can't interact using JavaScript or CSS.




回答2:


You may be able to do this by passing the data as

iframe.src = 'otherdomain/frame.html#bob@foo.com'

and using it in the iframe as

window.onload = function ()
{
     data = window.location.hash;
}

for more details read : http://msdn.microsoft.com/en-us/architecture/bb735305.aspx




回答3:


One trick that I've used to get around these little issues is sending an anonymous function to setTimeout():

var execMe = function() {$("email_frame").contents.find('input[name=email]').append("test");};
var result = setTimeout("execMe", 1000);

Disclaimer: I'm not certain this will work in this circumstance, and it is hacky. Use at your own risk. Just tossing around an idea :)



来源:https://stackoverflow.com/questions/2141535/change-text-box-value-of-an-iframe-on-a-different-domain-using-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!