user3110767
user3110767

Reputation: 21

is there a more dynamic way to call multiple a tags using php and javascript

Hello guys I am trying to find a more dynamic way to open multiple pop up windows one at a time in the same tab. here is my code below. it works ok but what if you have 10 a tags. any advice appreciated.

   <script>

function openwindow(){
    window.open("https://www.facebook.com/sharer/sharer.php?u=<?=urlencode($article['link'])?>&p[title]=<?=urlencode($article['a_headline'])?>&display=popup","width=100,hieght=100");
}

function openwindow1(){
    window.open("https://twitter.com/intent/tweet?text=<?=urlencode($article['a_headline'])?>&url=<?=urlencode($article['shorten'])?>&related=&via=proofEurope<?=urlencode($ks)?>","width=100,hieght=100");
}
</script>

<li><a href="javascript: openwindow()" rel="nofollow" target="_blank"><img src="<?=ASSETS?>/www/img/news/fb.png" /></a></li>

<li><a href="javascript: openwindow1()" rel="nofollow" target="_blank"><img src="<?=ASSETS?>/www/img/news/twt.png" /></a></li>

Upvotes: 0

Views: 42

Answers (1)

kainaw
kainaw

Reputation: 4334

First, you shouldn't use javascript in your href. It works, but that isn't the "proper" way to do it. Set href to # and put the javascript in onclick:

<a href='#' onclick='openwindow();'>my link</a>

You can call window.open in the onclick as well...

<a href='#' onclick="window.open('http://example.com');">my link</a>

Notice I had to double-quote the string containing single quotes to avoid escaping the quotes.

You have a logical combination of URLs and images. Match them up in an array. I'd make the image the key and the URL the value, as in:

$links = array(
    "fb.png" => "https://www.facebook.com/sharer/sharer.php?u=".urlencode($article['link'])."&p[title]=".urlencode($article['a_headline'])."&display=popup",
    "twt.png" => "https://twitter.com/intent/tweet?text=".urlencode($article['a_headline'])."&url=".urlencode($article['shorten'])."&related=&via=NewsweekEurope".urlencode($ks),
);

Now, you can loop through the array:

foreach($links as $img=>$url)
    print "<a href='#' onclick=\"window.open('$url')\"><img src='$img'></a>";

Upvotes: 1

Related Questions