Jump to content

J Query script Help !


kareem_elzeiny

Recommended Posts

Hi,I've found this code on the internet to open external links in new window

<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js' type='text/javascript'/><script type='text/javascript'>//<![CDATA[jQuery('a').each(function() {// Let's make external links open in a new window.var href = jQuery(this).attr('href');if (typeof href != 'undefined' && href != "" && (href.indexOf('http://') != -1 || href.indexOf('https://') != -1) && href.indexOf(window.location.hostname) == -1) {jQuery(this).attr("target", "_blank");}});//]]></script>

and since I don't know anything about j query I want a little help to modify this code and make it opens the external links in new window after a specified link, Ex. If I clicked on an external link this link will be opened automatically in new window after this code :(http://adf.ly/92161/External link here)I hope anybody can help me :)thanks in advanceRegards,

Link to comment
Share on other sites

The first thing I am seeing is how you are using "jQuery". Whenever you begin a jQuery statement, begin it with a $ sign.

<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js' type='text/javascript'/><script type='text/javascript'>//<![CDATA[$('a').each(function() {// Let's make external links open in a new window.var href = $(this).attr('href');if (typeof href != 'undefined' && href != "" && (href.indexOf('http://') != -1 || href.indexOf('https://') != -1) && href.indexOf(window.location.hostname) == -1) {$(this).attr("target", "_blank");}});//]]></script>

Link to comment
Share on other sites

Well ,,,,, I've just found this code I'm not the one who wrote it ! and I really don't know anything about "jQuery".I'm using this code on blogger I've just added it before </body> and it worked fine, but thanks for your advice :)can you help me to modify this code or even give me a new one that can achieve my goal ???regards,

Link to comment
Share on other sites

I would recommend reading some basic tutorials on jQuery. It isn't very hard. It uses CSS selectors (# = id, . = class, etc.), which are excellent alternatives to the clumsy Javascript "document.getElementById", not to mention that pure Javascript doesn't have a simple way of selecting classes (with the exception of IE9). If you are just trying to open links in a new window, why don't you just add the attribute "target='_blank'" to the anchor tag. More info here: http://www.w3schools.com/tags/att_a_target.asp

Link to comment
Share on other sites

So what exactly is it you're looking to do? If you just want to make all of your links open in a new window, then the code you posted above will do that. If you need to target specific links or groups of links please explain. Post samples of your HTML so we can see your structure.

Link to comment
Share on other sites

Well, I'm using blogger which uses xml and css codes the code that I've posted above is working perfectly in opening an external link in new window, ieIf my own blog domain is ( www.Myblog.com ) and I added this link ( http://youtubeload.blogspot.com/ ) in one of my posts,the above code will automatically opens it in a new window, but I need it to add another url before the external link "it's a URL protection service" so that it will be like that, iehttp://adf.ly/92161/ http://youtubeload.blogspot.com/the code will automatically modify [ <a href="http://youtubeload.blogspot.com/"></a> ] to [ <a href="http://adf.ly/92161/ http://youtubeload.blogspot.com/" target="_blank"></a>] without any manual interference from meSo, I need to add [http://adf.ly/92161/] automatically before any external link and then open it in a new window ,I hope you got what i mean Regards,

Link to comment
Share on other sites

Ok, I think I understood what you meant. You just need to modify the href value. This should do it:

if (typeof href != 'undefined' && href != "" && (href.indexOf('http://') != -1 || href.indexOf('https://') != -1) && href.indexOf(window.location.hostname) == -1) {$(this).attr("target", "_blank");$(this).attr('href', "http://adf.ly/92161/"+href); //This will prepend http://adf.ly/92161/ to the existing href value}

Link to comment
Share on other sites

if (href.search(/(google.com|yahoo.com)/) == -1) {   $(this).attr('href', "http://adf.ly/92161/"+href);}

That will search the href value for google.com or yahoo.com and if neither one is present in the string it will prepend your URL to the href.You can put anything you want inside the /(...)/ so long as you separate each value with a |.Edit:You need to escape the periods in the regex. Like so:/(google\.com|yahoo\.com)/Otherwise they'll match any character instead of a literal period.

Link to comment
Share on other sites

It didn't work with me, when I add it, it automatically exclude all the external links !Could you please show me where to place the previous code here

if (typeof href != 'undefined' && href != "" && (href.indexOf('http://') != -1 || href.indexOf('https://') != -1) && href.indexOf(window.location.hostname) == -1) {$(this).attr("target", "_blank");$(this).attr('href', "http://adf.ly/92161/"+href); //This will prepend http://adf.ly/92161/ to the existing href value}

Link to comment
Share on other sites

I think ShadowMage might have meant this:

if (href.search(/(google.com|yahoo.com)/) != -1) {   $(this).attr('href', "http://adf.ly/92161/"+href);}

Link to comment
Share on other sites

I think ShadowMage might have meant this:
if (href.search(/(google.com|yahoo.com)/) != -1) {   $(this).attr('href', "http://adf.ly/92161/"+href);}

No, that would prepend the URL if google and yahoo are present in the href. I believe the OP wants to prepend the URL if google or yahoo is not present in the href. Correct?
Link to comment
Share on other sites

my bad, I misread the page you linked to. I read it as returning -1 if a match was found, not when one isn't found. Anywho, carry on! :)

Link to comment
Share on other sites

thanks guys for helping me but I've told you I Don't know anything about J query or JAVA :) So the final code should be like that :

<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js' type='text/javascript'/><script type='text/javascript'>//<![CDATA[jQuery('a').each(function() {// Let's make external links open in a new window.var href = jQuery(this).attr('href');if (typeof href != 'undefined' && href != "" && (href.indexOf('http://') == -1 || href.indexOf('https://') != -1) && href.indexOf(window.location.hostname) != -1) {$(this).attr("target", "_blank");}if (href.search(/(google.com|yahoo.com)/) == -1) {$(this).attr('href', "http://adf.ly/92161/"+href);}});//]]></script>

It doesn't work like that , any help ???

Link to comment
Share on other sites

Because you moved it outside the original if statement. This is the code I gave you:

if (typeof href != 'undefined' && href != "" && (href.indexOf('http://') != -1 || href.indexOf('https://') != -1) && href.indexOf(window.location.hostname) == -1) {   $(this).attr("target", "_blank");   $(this).attr('href', "http://adf.ly/92161/"+href);}

Just wrap this line:$(this).attr('href', "http://adf.ly/92161/"+href);in the if statement I provided you with so you end up with something like this:

if (typeof href != 'undefined' && href != "" && (href.indexOf('http://') != -1 || href.indexOf('https://') != -1) && href.indexOf(window.location.hostname) == -1) {   $(this).attr("target", "_blank");   if (href.search(/(google\.com|yahoo\.com)/) == -1) {	  $(this).attr('href', "http://adf.ly/92161/"+href);   }}

Link to comment
Share on other sites

Because you moved it outside the original if statement. This is the code I gave you:
if (typeof href != 'undefined' && href != "" && (href.indexOf('http://') != -1 || href.indexOf('https://') != -1) && href.indexOf(window.location.hostname) == -1) {   $(this).attr("target", "_blank");   $(this).attr('href', "http://adf.ly/92161/"+href);}

Just wrap this line:$(this).attr('href', "http://adf.ly/92161/"+href);in the if statement I provided you with so you end up with something like this:

if (typeof href != 'undefined' && href != "" && (href.indexOf('http://') != -1 || href.indexOf('https://') != -1) && href.indexOf(window.location.hostname) == -1) {   $(this).attr("target", "_blank");   if (href.search(/(google\.com|yahoo\.com)/) == -1) {	  $(this).attr('href', "http://adf.ly/92161/"+href);   }}

Thank you it works, you were very helpful :)Regards,
Link to comment
Share on other sites

thanks guys for helping me but I've told you I Don't know anything about J query or JAVA :) So the final code should be like that :
<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js' type='text/javascript'/><script type='text/javascript'>//<![CDATA[jQuery('a').each(function() {// Let's make external links open in a new window.var href = jQuery(this).attr('href');if (typeof href != 'undefined' && href != "" && (href.indexOf('http://') == -1 || href.indexOf('https://') != -1) && href.indexOf(window.location.hostname) != -1) {$(this).attr("target", "_blank");}if (href.search(/(google.com|yahoo.com)/) == -1) {$(this).attr('href', "http://adf.ly/92161/"+href);}});//]]></script>

It doesn't work like that , any help ???

there is no JAVA involved, only Javascript.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...