English 中文(简体)
JavaScript函数在链接点击时执行?
原标题:Javascript function execution on link click?

我有一个链接,当用户单击它时,它会加载一个与正常页面不同的页面,并执行一个JS函数,在该页面的特定文本框中自动填充。使用Jquery还是JavaScript更好?我该如何使用其中任何一个来实现这一点?

最佳回答

你无法从源页面执行此操作。

这是一项安全功能。想象一下,如果您编写了一个JS函数,访问在线银行页面并使用用户当前的cookie自动填充银行转账。这就是为什么您不能这样做。

如果您控制另一页,则可以使用以下序列:

  • Save data to the server;
  • Go to the new page with a JS redirect;
  • The new page is loaded from the server;
  • While loading th epage the data that was saved from the server is retrieved and used to populate the text box.

因此,它可以从服务器上完成,但只有在那里保存它才可以完成。唯一的方法是使用Ajax。

另一种方法是:

  • Instead of a JS redirect, submit the page back to the server;
  • The server saves whatever data it needs to;
  • The server sends back an HTTP redirect to the new page;
  • The new page uses the saved data to construct the new page with the populated text box.
问题回答

在脚本的结尾添加return false;。这将使页面运行脚本而不会重定向页面。

编辑:(看到您的编辑后)。

使用JQuery还是JavaScript更好? 我如何使用其中之一来实现这个?

jQuery 是一个 JavaScript 库,因此只要你对结果感到满意,不管你使用纯 JavaScript 还是使用 jQuery,都没问题。

关于你所说你成功操纵了重定向页面的页面......我不明白如何可能。





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签