JavaScript的作品作为书签,但不作为的onclick链接(Javascript works

2019-10-17 03:31发布

我用下面的代码,允许用户添加一个书签到浏览器,它完美地工作,但如果我尝试从一个链接页面上实际发射的代码没有。

有什么不同,我需要添加/从代码中减去。

下面是我使用的:

javascript:(function(d){
var%20modal=document.createElement('iframe');
modal.setAttribute('src','http://url.com/bm.html?
url='+encodeURIComponent(window.location.href)+'&page_title='+document.title);
modal.setAttribute('scrolling','no');
modal.setAttribute('name','my_modal');
modal.className='modal';
document.body.appendChild(modal);
var%20c=document.createElement('link');
c.type='text/css';
c.rel='stylesheet';
c.href='//iframe.css';
document.body.appendChild(c);}(document));

我试着以下但不工作:

<a href="#" onclick="javascript:(function(d){var%20modal=document.createElement('iframe');modal.setAttribute('src','http://url.com/bm.html?url='+encodeURIComponent(window.location.href)+'&page_title='+document.title);modal.setAttribute('scrolling','no');modal.setAttribute('name','my_modal');modal.className='modal';document.body.appendChild(modal);var%20c=document.createElement('link');c.type='text/css';c.rel='stylesheet';c.href='//iframe.css';document.body.appendChild(c);}(document));">test</a>

Answer 1:

你必须:

  • 替换%20通过空间
  • 删除javascript:

你也可以使用这个函数调用招!function( ...

尝试这个:

<a href="#" onclick="!function(d){var modal=document.createElement('iframe');modal.setAttribute('src','http://url.com/bm.html?url='+encodeURIComponent(window.location.href)+'&page_title='+document.title);modal.setAttribute('scrolling','no');modal.setAttribute('name','my_modal');modal.className='modal';document.body.appendChild(modal);var c=document.createElement('link');c.type='text/css';c.rel='stylesheet';c.href='//iframe.css';document.body.appendChild(c);}(document)">test</a>


文章来源: Javascript works as bookmarklet but not as onclick link