Page 1 of 1

How to write a URL Opener using JavaScript

Posted: Wed Feb 17, 2010 3:24 am
by Neo
This is a simple script written by me on a forum request.

If you want to open urls in tabs instead of windows, see article at https://robot.lk/viewtopic.php?f=74&t=1286

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Free URL Opener</title>

<!-- Written by Neo @ ROBOT.LK 2010 -->

<script type="text/javascript">
<!--
function OpenURL(txid) {
    
    var lines;
    var TA = document.getElementById(txid).value;
    if(document.all) { // IE
        lines = TA.split("\r\n");
    }
    else { //Mozilla
        lines = TA.split("\n");
    }

    for(var i=0; i<lines.length; i++) {
        window.open(lines[i], 'example' + i);
    }
}
-->
</script>
</head>


<body>

<table width="400" border="0" align="center" cellpadding="3" cellspacing="1">
<tr>
<td><strong>Free URL Opener</strong></td>
</tr>
</table>

<table width="400" border="0" align="center" cellpadding="0" cellspacing="1">
<tr>
<td>
    
<form>
<table width="100%" border="0" cellspacing="1" cellpadding="3">

<tr>
<td>URL List</td>
<td>:</td>
<td><textarea name="urls" cols="50" rows="10" id="urls"></textarea></td>
</tr>

<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td><input type="button" value="Open" onclick="OpenURL('urls')" /><input type="reset" name="Submit2" value="Reset"></td>
</tr>

</table>
</form>

</td>
</tr>
</table>

</body>
</html>

Re: How to write a URL Opener using JavaScript

Posted: Sun Feb 05, 2012 10:45 am
by ashoksharmaz87
Thank you for your code.. its really very helpful
Its working fine if we use the complete URL with h t t p: // w w w. prefix

How can we modify this code to work even for the URLs without h t t p : // or w w w

awaiting your reply...

Thanks

Re: How to write a URL Opener using JavaScript

Posted: Mon Feb 06, 2012 12:53 am
by Saman
Just check whether the line has http prefix. If it is not available, just add it in the JavaScript code. Simple!

Re: How to write a URL Opener using JavaScript

Posted: Mon Feb 06, 2012 2:52 am
by SemiconductorCat
use this function for that. One thing just google before ask, this is the first search item of my googling search.

Code: Select all

function isUrl(s) {
	var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
	return regexp.test(s);
}

Re: How to write a URL Opener using JavaScript

Posted: Tue Mar 13, 2012 11:27 am
by lasyed
If it is not available, just add it in the JavaScript code. :D