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
- <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>