简体   繁体   中英

How to start js-function with arguments in HTML-page

I want to simplify the javascript in the HTML-page below. How do I start function FrameUpdate with 2 arguments? In this way I don't have to repeat the script for every HREF-action on my HTML-page.

Thanks, Sjoerd

<html>

<A HREF="geenscript.html" onclick="FrameUpdate3005(); return false;">Update 3005</A>
<p>
  <A HREF="geenscript.html" onclick="FrameUpdate3017(); return false;">Update 3017</A>


  <SCRIPT TYPE="text/javascript" LANGUAGE="JavaScript">
    <!--
    function FrameUpdate3005() {
      parent.boven.location.href = "3005-n.htm";
      parent.onder.location.href = "3005-t.htm";
    }
    //-->
  </SCRIPT>


  <SCRIPT TYPE="text/javascript" LANGUAGE="JavaScript">
    <!--
    function FrameUpdate3017() {
      parent.boven.location.href = "3017-n.htm";
      parent.onder.location.href = "3017-t.htm";
    }
    //-->
  </SCRIPT>

</html>

Something like this would do the job :

<a href="geenscript.html" onclick="FrameUpdate(3005); return false;">Update 3005</a>

<a href="geenscript.html" onclick="FrameUpdate(3017); return false;">Update 3017</a>
<script type="text/javascript">
    function FrameUpdate(value) {
        parent.boven.location.href = `${value}-n.htm`;
        parent.onder.location.href = `${value}-t.htm`;
    }
</script>

Just change FrameUpdate3005() to:

<A HREF="geenscript.html" onclick="FrameUpdate3005(param1Value,param2Value); return false;">Update 3005</A>

and

<SCRIPT TYPE="text/javascript" LANGUAGE="JavaScript">
<!--
function FrameUpdate3005(param1,param2) {
    // logic that uses param1 and param2
    parent.boven.location.href = "3005-n.htm";
    parent.onder.location.href = "3005-t.htm";
}
//-->
</SCRIPT>

Why not try to simply make it one function, instead of having a function for each? They seem to want the same result:

 function frameUpdate( id ) { parent.boven.location.href = id + "-n.htm"; parent.onder.location.href = id + "-t.htm"; // Since you always want to return false, why not wrap it in here? // No need to add a separate `return false`. return false; } 
 <a href="geenscript.html" onclick="return FrameUpdate( 3005 );">Update 3005</a> <a href="geenscript.html" onclick="return FrameUpdate( 3017 );">Update 3017</a> 

A couple of notes though, you are missing the <body> tag at least, and <head> should also be in there. And your <p> tag gets openened and never closed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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