0

the onlcick function is not working in the below case it always i have checked in ie8, and in some computer firefox and chrome also not working.

   $ad_div1='<div onclick="window.location=\''.$url.'\'">
   <a target="_blank" href="http://yahoo.com"> <img src="http://rose.com/abc.jpg"/></a>
    </div>';

but when i give an alert before window.location its working ie,

   $ad_div1='<div onclick="alert(1);window.location=\''.$url.'\'">
   <a target="_blank" href="http://yahoo.com"> <img src="http://rose.com/abc.jpg"/></a>
    </div>';

How to slove this problem?

Vitthal
  • 327
  • 1
  • 13
RoSe
  • 447
  • 4
  • 7
  • 20
  • are you getting any error in javascript console when you're trying with the first case? – anurupr Apr 09 '14 at 10:44
  • 3
    @govindsinghnagarkoti HTML is case-insensitive. – Teemu Apr 09 '14 at 10:46
  • may be due to double qoutes inside single qoutes – Govind Singh Apr 09 '14 at 10:49
  • 1
    Is that PHP you have? Because you can't concatenate strings using a `.` in javascript. If so, can you show the client-side generated script? – CodingIntrigue Apr 09 '14 at 10:50
  • @RGraham i have written it inside php code – RoSe Apr 09 '14 at 10:52
  • What you actually mean when you say "not working"? Nothing happens? Link opens a new page only? The window.location changes only? You can't see the content of `$ad_div1`? Something else? – Teemu Apr 09 '14 at 10:52
  • @Teemu i need to open yahoo.com in a new window and in the same window i need to go the link which i have given in $url – RoSe Apr 09 '14 at 10:54
  • Yes, but what happens instead? – Teemu Apr 09 '14 at 10:54
  • i need to track the link for that i am using onclick function and in the other page i need to open the link here it is yahoo.com – RoSe Apr 09 '14 at 10:56
  • 1
    @RoSe It's quite clear what you want, but you haven't described your problem. What happens that you don't want to happen? What your page does instead of what you want? – Teemu Apr 09 '14 at 10:58
  • @Teemu it displays ads according to javascript code – RoSe Apr 09 '14 at 11:00

2 Answers2

1

The code looks like it should produce code like this (when $url = "http://example.com" ):

<div onclick="window.location='http://example.com'">
    <a target="_blank" href="http://yahoo.com">
        <img src="http://rose.com/abc.jpg" />
    </a>
</div>

And this seems to work in Chrome and Firefox, see this jsfiddle. I'm guessing the $url variable is somehow wrong, look at the generated output of your code, does the onclick attribute look like my example the above?

EDIT:

Don't really understand why you want to do this, relying on javascript for tracking, and opening tabs/windows, etc...

Why not create a service on your server to do the tracking and redirect to the supplied url, so you could simplify the link like this:

$ad_div1='<a target="_blank" 
    href="http:yourdomain.com/redirect?target=' . 
    urlencode($actualTargetUrl) . '">
    <img src="http://rose.com/abc.jpg" />
</a>';

Then using php (or some other server script) or rewrite rule the to handle all /redirect* urls on yourdomain.com that would log/register the request and give a 302 "moved temporarily" redirect to the url specified by the target parameter.

Stein G. Strindhaug
  • 5,077
  • 2
  • 28
  • 41
0

And what exactly going on?

I suppose that when you click on link, browser fires event -> then event goes to yours div and eval code on yours "onclick" attribute -> then event goes to document and document change location based on your href attribute of your "a" element. And thats why your "onclick" not working.

You can try this solution:

onclick="var event = arguments[0] || window.event; event.stopPropagation();window.location='".$url."'"
KillaBee
  • 139
  • 7
  • Is there any solution for that? – RoSe Apr 09 '14 at 11:05
  • The `a` has `target="_blank"` which supposed to open the `href` in a new window/tab. Rather `window.location` in the `div` changes the location before execution catches the `a`...? – Teemu Apr 09 '14 at 11:06
  • You can try catch event and stop it propagation - event.stopPropagation(). – KillaBee Apr 09 '14 at 11:08
  • Oh, i understand.... You want open both locations? I am so stupid :( – KillaBee Apr 09 '14 at 11:20
  • @KillaBee yes, i want to open both locations – RoSe Apr 09 '14 at 11:25
  • 1
    @KillaBee I guess so (I mean opening the locations ; )), but OP refuses to tell, what's the actual problem. Maybe eventhandling model in IE8 fires `onclick` for the `div` first, "`some computer firefox and chrome`" probably have pop-up blocker on or sth... – Teemu Apr 09 '14 at 11:26