6

I am developing mobile web application using Sencha Touch (JavaScript & ExtJS). I have a link tag (just <a> tag with href), and I need to trigger click event on it.
I have found that call of myelement.dom.click(); do the job, but it's not working for Android... Is there some kind of universal solution?

UPDATE

I have a container with html code:

<a href="http://google.com" id="link_to_click" target="_blank">CLICK ME</a>

I need to simulate click on this link using only plain JavaScript and ExtJS. The link must be opened in new window/tab.

Andrey Rudenko
  • 1,271
  • 20
  • 34
  • Describe your problem more. You are using which browser on Android device. Which android. Write here code which is not workin. – Mertuarez Jan 29 '13 at 18:29

2 Answers2

9

Use the HTMLEvents object with document.createEvent() to simulate a click on a link.

var link = document.getElementById( 'link_to_click' ),
    event = document.createEvent( 'HTMLEvents' );

event.initEvent( 'click', true, true );
link.dispatchEvent( event );
ThinkingStiff
  • 64,767
  • 30
  • 146
  • 239
0

//android

WebView wv = (WebView) findViewById(R.id.webview);
wv.getSettings().setJavaScriptEnabled(true);

//javascript

function bindIcons() {

            jQuery("a.icons").bind('mouseover mouseout mousedown mouseup focus blur touchstart touchend', function (e){
              if (typeof console !== "undefined" ) console.log(e);
              if (e.type=='mouseover' || e.type=='focus') 
              {
                jQuery(e.currentTarget).toggleClass('over',true);
              }

              if (e.type=='mousedown' || e.type=='touchstart') 
              {
                jQuery(e.currentTarget).toggleClass('down',true);
              }

              if (e.type=='mouseout' || e.type=='mouseup' || e.type=='touchend' || e.type=='blur') 
              {
                jQuery(e.currentTarget).toggleClass('down',false);
                jQuery(e.currentTarget).toggleClass('over',false);
              }
            });

            if (isAndroid())
               jQuery("a.ifandroid").attr("href","javascript:ifAndroid();");
            else
               jQuery("a.ifandroid").toggleClass('ifandroid',false);

      }
Mertuarez
  • 901
  • 7
  • 24
  • I am unable to use jQuery. All I can use is plain JavaScript and ExtJS. And this is event binding, but I need event triggering (click simulation). – Andrey Rudenko Jan 30 '13 at 09:50