简体   繁体   中英

how to use window.open onClick - reactjs

I want to use window.open but I got this error message:

Uncaught Error: Expected onClick listener to be a function, instead got type string

my code:

<img id='drftrgvlnbpewmcswmcs' style={{cursor:'pointer'}} onClick='window.open("http://trustseal.enamad.ir/Verify.aspx?id=15288&p=nbpdyncrwkynaqgwaqgw", "Popup","toolbar=no, location=no, statusbar=no, menubar=no, scrollbars=1, resizable=0, width=580, height=600, top=30")' alt='' src='http://trustseal.enamad.ir/logo.aspx?id=15288&p=lznbfujyqesgukaqukaq'/>

You can't put plan string in onClick prop. You have to pass a function.

Example

<img id='drftrgvlnbpewmcswmcs' style={{cursor:'pointer'}} onClick={() => window.open("http://trustseal.enamad.ir/Verify.aspx?id=15288&p=nbpdyncrwkynaqgwaqgw", "Popup","toolbar=no, location=no, statusbar=no, menubar=no, scrollbars=1, resizable=0, width=580, height=600, top=30")} alt='' src='http://trustseal.enamad.ir/logo.aspx?id=15288&p=lznbfujyqesgukaqukaq'/>

As it says Uncaught Error: Expected onClick listener to be a function, instead got type string

It should be a function. So you can do the followings

openMyWindow = () => {
 window.open("http://trustseal.enamad.ir/Verify.aspx?id=15288&p=nbpdyncrwkynaqgwaqgw", "Popup","toolbar=no, location=no, statusbar=no, menubar=no, scrollbars=1, resizable=0, width=580, height=600, top=30")
}

and use it like

<img id='drftrgvlnbpewmcswmcs' style={{cursor:'pointer'}} onClick={this.openMyWindow} alt='' src='http://trustseal.enamad.ir/logo.aspx?id=15288&p=lznbfujyqesgukaqukaq'/>

or of course this way as mentioned in other answers

onClick={() => {
    window.open("http://trustseal.enamad.ir/Verify.aspx?id=15288&p=nbpdyncrwkynaqgwaqgw", "Popup", "toolbar=no, location=no, statusbar=no, menubar=no, scrollbars=1, resizable=0, width=580, height=600, top=30")
  }}

You need to enclose your code with a function. For example:

<img
  id='drftrgvlnbpewmcswmcs'
  style={{ cursor: 'pointer' }}
  onClick={() => {
    window.open("http://trustseal.enamad.ir/Verify.aspx?id=15288&p=nbpdyncrwkynaqgwaqgw", "Popup", "toolbar=no, location=no, statusbar=no, menubar=no, scrollbars=1, resizable=0, width=580, height=600, top=30")
  }}
  alt=''
  src='http://trustseal.enamad.ir/logo.aspx?id=15288&p=lznbfujyqesgukaqukaq'
/>

i hope the code can be usefull for you

 <img
      id='drftrgvlnbpewmcswmcs' 
      style={{cursor:'pointer'}} 
      onClick={()=>{ window.open("http://trustseal.enamad.ir/Verify.aspx?id=15288&p=nbpdyncrwkynaqgwaqgw", "Popup","toolbar=no, location=no, statusbar=no, menubar=no, scrollbars=1, resizable=0, width=580, height=600, top=30") }} 
      alt=''
      src='http://trustseal.enamad.ir/logo.aspx?id=15288&p=lznbfujyqesgukaqukaq'
 />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM