July 2, 20188 yr Hi all, this is what i would like to do 1. when window size is < 1000, change any plain text url to a clickable one 2. when window size is > 1000, change any plain text url to a clickable one, replaced by the word "Link". I had a go but it seems to be failing somewhere! Anyone can shed some light? Thanks HTML <html> <head> <script src="https://code.jquery.com/jquery-3.1.0.js"></script> <script>$(document).ready(function() { function checkWidth() { var windowSize = $(window).width(); if (windowSize < 1000) { $('div').each(function(){ $(this).html( $(this).html().replace(/((http|https|ftp):\/\/[\w?=&.\/-;#~%-]+(?![\w\s?&.\/;#~%"=-]*>))/g, '<a href="$1">$1</a> ') ); }); } //make plain text url clickable else if (windowSize < 1000) { $('div').each(function(){ $(this).html( $(this).html().replace(/((http|https|ftp):\/\/[\w?=&.\/-;#~%-]+(?![\w\s?&.\/;#~%"=-]*>))/g, '<a href="Link">Link</a> ') ); }); } //convert plain text to clickable word "Link" } checkWidth(); $(window).resize(checkWidth); $(window).onload(checkWidth); }); </script> </head> <body> <div>https://www.google.com/</div> <div>https://www.yahoo.com/</div> </body> </html>
July 3, 20188 yr Author Nice solution from @jakecigar in the jquery forums HTML: <div>https://www.google.com/</div> <div>https://www.yahoo.com/</div> CSS: @media only screen and (max-width: 1000px) { .abbr { font-size:0; text-decoration:none; } .abbr:after { display: inline; font-size:16px; content: "link"; color:black; } } JS: $(function() { $('div').each(function() { $(this).html($(this).html().replace(/((http|https|ftp):\/\/[\w?=&.\/-;#~%-]+(?![\w\s?&.\/;#~%"=-]*>))/g, '<a class=abbr href="$1">$1</a> ')); }); }) Edited July 3, 20188 yr by 98sfsdf8
Create an account or sign in to comment