在带有动态内容的新标签中打开 [英] Open in new tab with dynamic content
问题描述
$ b
现在,当用户右键点击其中一个较小的广告并希望在新标签中打开它(作为主广告),所以我需要的是在新的选项卡上有一个?id ='id of ad'参数。
所以广告有一个onlick(它运行javascript来切换它主广告),并有一个新的标签中打开右键点击新的选项卡中的参数加载相同的页面?
就像是有一个jQuery的功能,这将是令人惊叹的。
您需要使用hashtag函数并在URL上附加一个散列值。链接如:
< a href =http://your-site.com/page#advert1>广告1·/ a>
< a href =http://your-site.com/page#advert2>广告2< / a>
<! - Ad3,Ad4等 - >>
以下是可用于读取hashtag的jQuery示例:
$ b $如果(window.location.hash.length> 2){/
/ / GET HASHTAG名称
var advert = window.location.hash.replace('#','').replace(/ \ / $ /,'').substr(1);
//然后使用此信息从您的阵列获取广告内容的电话功能
}
或者,您可以使用一个插件,例如 jQuery地址,它是为这个确切的事情做了准备,并且加载了更多的功能,包括捕获Google Analytics的hashchange事件和点击跟踪。
for this example I'm going to use a property site. If at the top of the site I had the main viewed property ad, and below a whole lot of small square divs with the other ad's. Now when you click on one of the smaller ads it swaps place with the main ad (dynamic content change using JavaScript, all ad data exists in JavaScript multidimensional array and is loaded from sql via php via ajax on page load.
Now when a user right clicks on one of these smaller ads and want to open it in a new tab (as the main ad) so all I need is to load the same page in the new tab with a ?id='id of ad' argument to do so.
so how do it so the ad has an onlick (which runs javascript to switch it with the main ad) and have a right click open in new tab which loads same page with the argument in the new tab?
like if there was a jquery function for this would be amazing.
You need to use a hashtag function and append a hash value onto the URL. So the square divs would actually be links like:
<a href="http://your-site.com/page#advert1">Ad 1</a>
<a href="http://your-site.com/page#advert2">Ad 2</a>
<!-- Ad3, Ad4 etc. -->
Here's an example of the jQuery you could use to read the hashtag:
// CHECK IF A HASHTAG EXISTS IN THE URL
if(window.location.hash.length > 2) {
// GET THE HASHTAG NAME
var advert = window.location.hash.replace('#', '').replace(/\/$/, '').substr(1);
// THEN CALL FUNCTION TO GET AD CONTENT FROM YOUR ARRAY USING THIS INFO
}
Alternatively you could use a plugin for this like jQuery Address which is made for this exact thing and has a load more features including the capture of hashchange events and click tracking for Google Analytics.
这篇关于在带有动态内容的新标签中打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!