Search code examples
javascriptchromiumchromium-embedded

Keep the page active after run JS code in the URL


I use DCEF3, a Delphi wrapper for Chromium. I run a lot of JS code on my app to interact with pages, like the example below :

document.getElementById('name').value = 'Test';

To do so, i call the 'load' method of Chromium and run the code below in the URL field :

javascript:document.getElementById('name').value = 'Test';

It's the same thing as i open Chrome and paste the code above in the URL field. It will work, but the problem is after i run the command, the browser replaces the current page for a blank one with the content 'Test'.

The only way i could find to avoid this issue, to fill the element and keep the page active, is to run an additional 'focus' command in the same URL, like this :

javascript:document.getElementById('name').value = 'Test';
document.getElementById('name').focus(); 

The additional 'focus' method seems to force Chrome to keep the current page active, instead of replacing it for a blank one.

Is this approach correct ? Is there a better way to achieve this task ?

Thanks !


Solution

  • There is no need to call focus.

    Looks like now it's not possible to disable redirect by just returning false at the end.

    Still, you can replace focus with void(0);

    javascript:document.getElementById('name').value = 'Test'; void(0)
    

    void(0) was popular idiom to indicate that link is not pointing any where. It's reare to see et now, but it still working in browser URL.

    By the way, "javascript:" in URL is disabled in most browsers.