Linkify (plain url to link) based on windowSize

Linkify (plain url to link) based on windowSize

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

  1. <html>
  2. <head>
  3.    <script src="https://code.jquery.com/jquery-3.1.0.js"></script>
  4.    <script>$(document).ready(function() {
  5.     function checkWidth() {
  6.         var windowSize = $(window).width();
  7.         if (windowSize < 1000) {
  8.           $('div').each(function(){ $(this).html( $(this).html().replace(/((http|https|ftp):\/\/[\w?=&.\/-;#~%-]+(?![\w\s?&.\/;#~%"=-]*>))/g, '<a href="$1">$1</a> ') ); });
  9.         } //make plain text url clickable
  10.         else if (windowSize < 1000) {
  11.           $('div').each(function(){ $(this).html( $(this).html().replace(/((http|https|ftp):\/\/[\w?=&.\/-;#~%-]+(?![\w\s?&.\/;#~%"=-]*>))/g, '<a href="Link">Link</a> ') ); });
  12.         } //convert plain text to clickable word "Link"
  13.         
  14.     }
  15.     checkWidth();
  16.     $(window).resize(checkWidth);
  17.     $(window).onload(checkWidth);
  18. });
  19.   </script>  
  20. </head>  
  21. <body>
  22.   <div>https://www.google.com/</div>
  23.   <div>https://www.yahoo.com/</div> 
  24.   </body>
  25. </html>