how can I add
<a href="javascript:function foo(){alert('hi');}" title="alert">
Drag to your bookmarks bar
</a>
in my wordpress post.
I have built a bookmarklet and i want to pass it through my blog. But wordpress is stripping out the javascript from my post when it displays it.
Javascript in WordPress Single Pages and Posts
Source: https://codex.wordpress.org/Using_Javascript
The issue is likely to be caused at the browser end. Chrome's XSS was the problem for me and I solved it by adding the line
header ("X-XSS-Protection: 0");
to wp-blog-header.php in the root folder of my installation. This may not be ideal as it applies across the whole site. It would probably be better to add it where it would only apply to the post/page which needs to render the bookmarklet.This is old, but still relevant with Version 4.9.5 for wordpress, so I answer with my solution for this:
Wordpress filters out any Javascript you use in your Posts or pages, that is why your code gets lost. I did the following steps to add a "javascript:" Link:
The important part is adding the on-Handler to the Link you want to use. Now the Javascript is loaded right after the page got loaded. And a click on the link will call the function yourFunction
I have no issues embedding that code in the HTML editor on my wordpress site. There is a problem with your javascript code- that defines a function, but never calls it. I have
in a post, and I get the alert when click, as well as the bookmarklet.