简体   繁体   English

执行此特定 onclick 事件时如何执行 JavaScript 函数?

[英]How to perform a JavaScript function when this specific onclick event is performed?

I am not so into JavaScript and I have the following problem.我不太喜欢 JavaScript,我有以下问题。

Into a JSP page I have some "links" like this:进入 JSP 页面,我有一些像这样的“链接”:

<tr onmouseout="Javascript: this.style.background='#EAEFFF'; this.style.color='#003399';" onmouseover="Javascript: this.style.background='#003399'; this.style.color='#FFFFFF'; this.style.cursor='hand';" style="background-color: #EAEFFF;" onclick="Javascript: document.location.href='edi.do?serv=4.1';">
    <td>
        <!--A HREF="edi.do?serv=4.1" class="linkBlue" onMouseOver="self.status='Comunicazioni'; return true"-->
        Comunicazioni
    </td>
</tr>

As you can see in the previous code snippet the link is not yet implemented by a classic a href but, in its place, it is done by JavaScript using this expression:正如你在前面的代码片段中看到的,链接还没有通过经典的a href实现,而是由 JavaScript 使用以下表达式实现:

onclick="Javascript: document.location.href='edi.do?serv=4.1';"

So, correct me if I am saying wrong assertion, I think that (when the user click on the tr ) by the Javascript: it perform the following JavaScript operation that open the link.因此,如果我说错误的断言,请纠正我,我认为(当用户单击tr 时)通过Javascript:它执行以下打开链接的 JavaScript 操作。 Is it the correct interpretation?这是正确的解释吗?

It works but my problem is that, before doing it, I have to perform a specific loadingPopUp() JavaScript function defined into the它的工作原理,但我的问题是,在这样做之前,我必须执行特定loadingPopUp() JavaScript函数定义成

<script type="text/javascript">...</script>

section of my page.我页面的部分。 So my problem is: how can I perform the loadingPopUp() function before the Javascript: document.location.href='edi.do?serv=4.1';所以我的问题是:如何在Javascript: document.location.href='edi.do?serv=4.1';之前执行loadingPopUp()函数Javascript: document.location.href='edi.do?serv=4.1'; on the onclick event? onclick事件?

change onclick event to:onclick事件更改为:

onclick="changeLocationTo('edi.do?serv=4.1')"

create a new js function:创建一个新的js函数:

function changeLocationTo(newLocation)
{
   loadingPopUp();

   // you may add an timeout here or to handle a popup action like closing it

   document.location.href = newLocation;
}

Yes, Your assertion is absolutely right.是的,你的说法完全正确。 But I think if you call your function before changing url then it would not be correct approach because second function will be executed immediately after your loadingpopup function and will redirect user to different page, so I think your purpose of opening popup will not be fulfilled.但是我认为如果您在更改 url 之前调用您的函数,那么这将不是正确的方法,因为第二个函数将在您的loadingpopup函数之后立即执行并将用户重定向到不同的页面,所以我认为您打开弹出窗口的目的将无法实现。

So try this所以试试这个

Way 1: if you want to load your popup and immediately redirect user方式 1:如果您想加载弹出窗口并立即重定向用户

function fnOpenNewLink(){
    loadingPopUp();
    document.location.href='edi.do?serv=4.1';
}

Way 2: if you want to load your popup and then redirect user according to your requirement.方式2:如果您想加载弹出窗口,然后根据您的要求重定向用户。

function fnOpenNewLink(){
    loadingPopUp(function(){
        document.location.href='edi.do?serv=4.1';
    });
}

function loadingPopUp(callback){

    //your stuff

    //when everything is done, just call the callback
    callback();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM