Call href from JavaScript

坚强是说给别人听的谎言 提交于 2019-12-05 21:01:38

问题


This is the same question as THIS ONE, I can't answer that anymore, so I'm re-posting it with my account.
Sorry for the mess.

I need a Greasemonkey script that on a page load activates a href link like 'javascript:FUNCTION'. I've seen this code:

<script language="Javascript" type="text/javascript">
    function somescript() {
            window.location.href = document.getElementById('ololo').href;
    }
</script>

<a href="javascript:alert('test');" id="ololo">test</a>
<br />

<a href="javascript:somescript()">click me</a>

and, while it works on a local page even when using onload, it doesn't work when I use it in my script.

Probably I'm missing something when transferring the code from the body of an html page to a Greasemonkey script.

I hope this time the question is more clear, excuse me for any misunderstanding, but I'm still a beginner with JS.


回答1:


Solved it like this:

window.location=document.getElementById('foo').href;

Thanks everyone for answering anyway.




回答2:


<script type="text/javascript">
    function somescript() {
        eval(document.getElementById('ololo').getAttribute('href').replace('javascript:', ''));
    }
</script>

I can see the alert box..

Please note that this will only work when its javascript code into the href attribute...




回答3:


Will this work for your scenario?

<script type="text/javascript">
  function somescript() {
    document.getElementById('ololo').click();//fake a click on the link
  }
</script>


来源:https://stackoverflow.com/questions/1783084/call-href-from-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!