How to write a URL Opener using JavaScript

Web programming topics
Post Reply
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

How to write a URL Opener using JavaScript

Post by Neo » Wed Feb 17, 2010 3:24 am

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>
User avatar
ashoksharmaz87
Posts: 1
Joined: Sun Feb 05, 2012 10:37 am

Re: How to write a URL Opener using JavaScript

Post by ashoksharmaz87 » Sun Feb 05, 2012 10:45 am

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
User avatar
Saman
Lieutenant Colonel
Lieutenant Colonel
Posts: 828
Joined: Fri Jul 31, 2009 10:32 pm
Location: Mount Lavinia

Re: How to write a URL Opener using JavaScript

Post by Saman » Mon Feb 06, 2012 12:53 am

Just check whether the line has http prefix. If it is not available, just add it in the JavaScript code. Simple!
User avatar
SemiconductorCat
Major
Major
Posts: 455
Joined: Mon Aug 22, 2011 8:42 pm
Location: currently in hyperspace

Re: How to write a URL Opener using JavaScript

Post by SemiconductorCat » Mon Feb 06, 2012 2:52 am

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);
}
User avatar
lasyed
Posts: 1
Joined: Tue Mar 13, 2012 11:22 am
Location: http://www.slimx-treme.com

Re: How to write a URL Opener using JavaScript

Post by lasyed » Tue Mar 13, 2012 11:27 am

If it is not available, just add it in the JavaScript code. :D
Post Reply

Return to “Web programming”