OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Javascript function works in console but not as bookmarklet

  • Thread starter Thread starter Wolfgang Braun
  • Start date Start date
W

Wolfgang Braun

Guest
I want to change dropdown values in a page. It uses frames, so I'm doing this:

Code:
javascript: (() => {
  iframe = document.getElementsByName('rechts')[0]
  frameDocument = iframe.contentDocument || iframe.contentWindow.document;
  dropdowns = frameDocument.getElementsByTagName('select');
  for (i = 0; i < dropdowns.length; i++) {
    dropdowns[i].value = 'f'; 
  }
})();

This works fine from console in firefox.

If I put it into a bookmarklet I get the following:

Uncaught SyntaxError: unexpected token: identifier

What is the issue, and how can I fix it?

One suggestion on stackoverflow was to put the code on a single line and URL encode:

Code:
javascript:%20(()%20=%3E%20%7B%20iframe%20=%20document.getElementsByName('rechts')%5B0%5D%20frameDocument%20=%20iframe.contentDocument%20%7C%7C%20iframe.contentWindow.document;%20dropdowns%20=%20frameDocument.getElementsByTagName('select');%20for%20(i%20=%200;%20i%20%3C%20dropdowns.length;%20i++)%20%7B%20dropdowns%5Bi%5D.value%20=%20'f';%20%7D%20%7D)();%0A

But it still yields the same error.

<p>I want to change dropdown values in a page. It uses frames, so I'm doing this:</p>
<pre><code>javascript: (() => {
iframe = document.getElementsByName('rechts')[0]
frameDocument = iframe.contentDocument || iframe.contentWindow.document;
dropdowns = frameDocument.getElementsByTagName('select');
for (i = 0; i < dropdowns.length; i++) {
dropdowns.value = 'f';
}
})();
</code></pre>
<p>This works fine from console in firefox.</p>
<p>If I put it into a bookmarklet I get the following:</p>
<blockquote>
<p>Uncaught SyntaxError: unexpected token: identifier</p>
</blockquote>
<p>What is the issue, and how can I fix it?</p>
<p>One suggestion on stackoverflow was to put the code on a single line and URL encode:</p>
<pre><code>javascript:%20(()%20=%3E%20%7B%20iframe%20=%20document.getElementsByName('rechts')%5B0%5D%20frameDocument%20=%20iframe.contentDocument%20%7C%7C%20iframe.contentWindow.document;%20dropdowns%20=%20frameDocument.getElementsByTagName('select');%20for%20(i%20=%200;%20i%20%3C%20dropdowns.length;%20i++)%20%7B%20dropdowns%5Bi%5D.value%20=%20'f';%20%7D%20%7D)();%0A
</code></pre>
<p>But it still yields the same error.</p>
 

Latest posts

Online statistics

Members online
0
Guests online
3
Total visitors
3
Ads by Eonads
Top