2
            <script type="text/javascript"> 
            var urlmenu = document.getElementById( "rpdropdown3" );
            urlmenu.remove(1);
            urlmenu.onchange = function() {
            window.open( this.options[ this.selectedIndex ].value, "_blank" );
            var options = rpdropdown3.options;
                // Look for a default selected option
                for (var i=0, iLen=options.length; i<iLen; i++) {

                    if (options[i].defaultSelected) {
                        rpdropdown3.selectedIndex = i;
                        return;
                    }                                                 
                }

                // If no option is the default, select first or none as appropriate
                selectElement.selectedIndex = 0; // or -1 for no option selected
            };

            </script>';

$postslist = get_posts( $args );
    foreach( $postslist as $posts ){
        $string .= '<option value="' . get_the_permalink($posts) . '">-- '  .get_the_title($posts).'</option> ';
    }

Doesn't work on any mobile browser (works on computers browsers) , the select dropdown doesn't open the link as expected

DrK
  • 144
  • 1
  • 10
  • Probably irrelevant to your issue, but why `this.options[this.selectedIndex].value` and not just `this.value`? – RobG Oct 14 '19 at 14:09
  • You want to get the selected index value that's why – DrK Oct 14 '19 at 14:41
  • They do the same thing, only one is a lot less code. *this.value* returns the value of the select element, which is the value of the selected option (which is its value or text if it has no value attribute or property) or "" (empty string) if no option is selected. Using the *selectedIndex* method, if no option is selected, an error is thrown (as there is no option at index -1, so `this.options[this.options.selectedIndex]` returns undefined, and attempting to access the *value* property of *undefined* is a type error). – RobG Oct 15 '19 at 00:58
  • Thanks for the explanation @RobG I changed the code but the problem is still around – DrK Oct 15 '19 at 07:55

1 Answers1

0

It seems that It's actually working but it's blocked as a popup. You can't use window.open if it's not fired by a user action, like a button or a link.

foreach ($postslist as $posts) { ?>
    <select id="rpdropdown" onchange="if (this.value) window.location.href=this.value">
        <option value="" selected disabled>OPTION 1</option>
        <option value="<?php echo get_the_permalink($posts) ?>" onchange="window.open (this.value)"> -- <?php echo get_the_title($posts) ?></option>
    </select>

<?php }

In my case I used onchange="if (this.value) window.location.href=this.value" and I'm trying to make it open in a new tab.

They talk about that issue here and on others many posts : javascript window.open in safari

DrK
  • 144
  • 1
  • 10