Change the selectMenu option programmatically in child inAppBrowser

I open a window using javascript window.open()

The window that opens has the following code:

 jQuery(document).ready(function(){ jQuery("#lang").change(function(){ var lname = jQuery(this).val().split("-"); window.location = '<?php echo JURI::root();?>index.php?lang='+lname[0]; alert(lname[0]); alert('lang '+lang); }); 

This code now runs when the "lang" menu is changed. I open the window programmatically using window.open , and I managed to populate the data in the window fields using Window.executeScript() . For example, this works for me:

 loginWindow.executeScript({ code: "jQuery('input#username').val('10500050')" }); 

However, when I tried to follow the same logic to change the selected item in selectMenu called 'lang' for the same window, I failed.

Attempts

I tried all of the following lines in executeScript ;

 code: "$('#lang').val('ms')" code: "jQuery('#lang option[value=ms]').prop('selected', true)" code: "jQuery('#lang').selectmenu('value', 'ms')" 

with them in the next executeScript to trigger a change

 code: "$('#lang').trigger('change')" code: "$('#lang').selectmenu('refresh', true)" code: "jQuery('#lang').selectmenu('refresh')" code: "jQuery('#lang').selectmenu('change')" code: "$('#lang').change()" 

None of them helped. I'm not sure if I should combine them into one executeScript . I do not know how to do that. A window opens from another domain.

Did I miss something?

All code on the side of the opening device is as follows:

 loginWindow.addEventListener( 'loadstop', function() { alert('test'); var loop = setInterval(function() { loginWindow.executeScript({ code: "jQuery('input#username').val('10500050')" }, function( values ) { var give = values[ 0 ]; if ( give ) { clearInterval( loop ); giveMeUsername(); } }); loginWindow.executeScript({ code: " jQuery('input#name').val('10500050')" }, function( values ) { var give = values[ 0 ]; if ( give ) { clearInterval( loop ); giveMeUsername(); } }); loginWindow.executeScript({ //code: "$('#lang').val('zh')" //code: "jQuery('#lang option[value=ms]').prop('selected', true)" code: "jQuery('#lang').selectmenu('value', 'ms')" //code: "localStorage.setItem( 'lan', 'ms' )" }, function( values ) { var give = values[ 0 ]; if ( give ) { clearInterval( loop ); giveMeUsername(); } }); loginWindow.executeScript({ //code: "$('#lang').trigger('change')" //code: "$('#lang').selectmenu('refresh', true)" //code: "jQuery('#lang').selectmenu('refresh')" //code: "jQuery('#lang').selectmenu('change')" code: "$('#lang').change()" }, function( values ) { var give = values[ 0 ]; if ( give ) { clearInterval( loop ); giveMeUsername(); } }); }); }); 

The first two executeScript work fine. But the last two (selectMenu part) do not work. Nothing happens.

UPDATE

When you add this code to the window itself, it works, and the value of the selection field changes:

 jQuery("#lang option[value='zh-TW']").attr("selected","selected"); jQuery('#lang').change(); 

However, when I add it inside executeScript in the parent window (opener), it does not work!

+6
source share
1 answer

Since the code works when I paste it directly into the child window inside jQuery(document).ready(function() , I assumed that it should work from the parent window using executeScript . After many trial and trial errors, the following code worked for me:

  loginWindow.executeScript( { code: "jQuery('#lang option[value=zh-TW]').attr('selected','selected'), jQuery('#lang').change()" }); 

This is the same code that I mentioned in the β€œUpdate” part of my question, except for the difference in a single quote and a double quote. Here, using executeScript , I had to manipulate to use a double quote just to executeScript all the code I entered, and the rest I use a single quote.

In the above code, change the parameter selected in the selection window in the child window using the executeScript method in the parent window (opener).

+1
source

Source: https://habr.com/ru/post/986632/


All Articles