Search code examples
javascriptionic-frameworklocal-storageinappbrowser

How I can pass a value in localStorage to InAppBrowser using executescript event


I have a problem with the plugin InAppBrowser.

I would like to insert a value stored in localStorage from my ionic app in a form on my website (username and password). It works fine with a simple value like 'test', but when I want to insert a value like localStorage.getItem('onlineIdStore') nothing is happening. Do you have a solution?

I found a similar problem but the solution does not work for me: Autologin with Inappbrowser on an external website

Here is my code:

var ref = window.open('website', '_blank', 'location=no');
ref.addEventListener('loadstop', function(event) {
ref.executeScript({ code: "document.getElementById('doc_login_username').value = 'test' " }); 
   });

Solution

  • Your problem isn't localStorage it's your string your variables are in your current script so you need to evaluate the variables before its sent to the new window like this

    var ref = window.open('website', '_blank', 'location=no');
    ref.addEventListener('loadstop', function(event) {
    ref.executeScript({ code: "document.getElementById('doc_login_username').value = '"+username+"'; " }); 
    });