简体   繁体   中英

Link to a JavaScript function via href

I'm struggling in linking an HTML link <a> with a JavaScript function using Google Apps Script. I tried the following, but it left me with this error message:

"Unsafe JavaScript attempt to initiate navigation for frame with origin ' https://script.google.com ' from frame with URL 'different url'. The frame attempting navigation must be same-origin with the target if navigating to a javascript: url"

My code:

var link = document.createElement("a");
link.setAttribute("href", "javascript:test()");
link.textContent = "Click";

//----------------------------------------------
function test() {
    console.log("Test");
}

The code is placed in an HTML file between:

<script>
</script>

Everything is coded in the IDE of Google Apps Script.

don't use href. Use the onclick event for JavaScript functions.

Use the onclick javascript function instead:

<a onclick="myFunction()">Click me</a>

https://www.w3schools.com/jsref/event_onclick.asp

href is an attribute used to define the target of the link. The right attribute to use for click-triggered events is onclick .

See the W3C documentation for href and onclick

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