2013-07-06 16 views
0

场景:你有一个脚本,用户可以在其网站上放置,当他们点击它,它重定向到我的网站,然后只调用后,他们已经成功地重定向到我的网站的功能和作用是部分我网站,所以相同的原产地安全政策应该没有任何问题。Javascript或jquery:你可以加载一个页面然后调用一个函数吗?

所以是该方案可能吗?

编辑

现在好了,我知道这是可以做到的,我碰到一个咸菜这样做。

function main(){ 
$(document).ready(function($){ 
window.location.href = 'http://www.example.com/michael'; 
theclient.chat(); 

}); 
} 

我希望在加载example.com/michael之后调用theclient.chat(),但它不起作用。

更新2

function main(){ 
window.location.href = 'http://www.reflap.com/michaelnana'; 

$(document).ready(function(){ 
theclient.chat(); 

}); 

} 

所以将这项工作?

+2

是当然的。当浏览器加载你的页面时,它就是你的页面,就好像用户点击了一个简单的链接(这实际上就是你所描述的)。 – Pointy

+2

我觉得关键是要在新网站上调用JS函数 –

回答

2

你必须调用该函数在自己的网站在以下块:

来源页面:

function main(){ 
    window.location.href = 'http://www.example.com/michael'; 
} 

目标页面(http://www.example.com/michael):

$(document).ready(function(){ 
    theclient.chat(); 
}); 

要明确:这会被调用,如果你输入的页面的URL也不只是在重定向后。

您应该添加URL参数,当你重定向,如果你只想要一个重定向后调用它。

UPDATE: 不能调用原始网页上的功能,重定向已经完成之后。

+0

感谢Balint的初步答复。我更新了这个问题,因为我认为你可以根据更新给我一个更好的答案。 –

+0

'theclient.chat();'行不会仅仅因为你已经被重定向而被执行(所以旧页面被卸载) –

+0

是的,但是theclient.chat()不是旧页面的函数。这是example.com的一个功能。你明白我的意思吗? –

1

在您的目标页面,如果包括jQuery库,使用此代码:

$(document).ready(function(){ 
theclient.chat(); 
}); 

ready()方法可以确保网页(http://www.reflap.com/michaelnana)运行你的JavaScript渲染前。

0

我有一个应作为你想要做什么骨架3个示例文件。

www.external-site.com/index.html

<!DOCTYPE html> 
<html lang="en"> 
    <head> 
    <meta charset="utf-8"/> 
    <title>3rd Party Page</title> 
    </head> 
    <body> 
    <script type="text/javascript" 
     src="http://www.example.com/michael/embed.js"> 
    </script> 
    </body> 
</html> 

www.example.com/michael/embed.js

// load jQuery, keep it in our scope 
// I'll not explain how this works but if you're making an embed 
// script for other sites, you must make sure to encapsulate all 
// your dependencies such as jQuery. 
loadDependencies(function($) { 
    // document onload 
    $(function() { 
    // create a button that redirects to example.com/michael 
    var button = $('<a>').text('click me').click(function() { 
     window.location.href = 'http://www.example.com/micahel'; 
    }); 

    // insert that button after this script tag 
    var src = 'http://www.example.com/michael/embjed.js'; 
    $('script[src="' + src + '"]').after(button); 
    }); 
}); 

www.example.com/michael/index。 HTML

<!DOCTYPE html> 
<html lang="en"> 
    <head> 
    <meta charset="utf-8"/> 
    <title>Landing Page</title> 
    <script type="text/javascript" src="jQuery.js"></script> 
    <script type="text/javascript" src="theClient.js"></script> 
    <script type="text/javascript"> 
     $(function() { 
     // all visitors to this page will trigger this call, not just 
     // the ones who came from the script embed. If you want to 
     // differentiate I'd recommened adding a query paremeter to 
     // the redirect and reading it there. 
     theClient.chat(); 
     }); 
    </script> 
    </head> 
    <body> 

    </body> 
</html> 
+0

你可能是指'embed.js'而不是'embjed.js' –

相关问题