javascript - How to respond to an event after loading a hyperlink page? -
i wonder possible respond event after client click hyperlink , hyperlink loaded? event can use properties in hyperlink(i.e. id, class...etc)?
for example, page_a have hyperlink
<a href="page_b" onclick="some_func">link</a>
but since some_func need use properties in page_b, let page_b have line
<p id="a">hello world</p>
and some_func want it(e.g. document.getelementbyid("a")
), how can first load hyperlink(page_b) run some_func?
you use localstorage save name of function want execute on second page, , call once loads.
the code this:
js
// sample function called function myfunc() { alert("called previous page!"); } // save name of function in local storage function saveforlater(func) { localstorage.setitem("func", func); } // if function exists if (localstorage.func) { // call (not using evil eval) window[localstorage.func](); // remove storage next page doesn't execute localstorage.removeitem("func"); }
html (just testing)
<a href="test2.html" onclick="saveforlater('myfunc')">go page 2</a><br/> <a href="test2.html">go page 2 (not saving function)</a>
notice: code runs on client's side, subject changed user, must careful on how proceed , execute "saved" function, or may find security problem.
Comments
Post a Comment