Forum Moderators: open

Message Too Old, No Replies

Split Functions

         

gaucho

5:57 pm on May 14, 2005 (gmt 0)

10+ Year Member



Hi,

Here is 6 functions gathered from Internet, but nothing works in tha way I want.

Sorry, by my lot of ignorance in JS - In my time as programmer there was not Windows...

Well... between you and me I need your expertise in JS for help me in the following - simple - question:

How I do for these functions reading and stripping URLS from <iframes> or <textarea>?

Functions made for stripping urls!

1.
<script language="JavaScript" type="text/javascript">
<!--

var string0='http://wwwwwww/ww.htm';
var string1='http://wwwwww/www.html';
var string2='http://wwwwwwwww.xml';
var string3='http://wwwwwwwww./ ';
var stringstart,stringenddot,stringlstslash;
var Ary=new Array(string0,string1,string2,string3);

for (i=0;i<Ary.length;i++){
stringstart=Ary[i].substring(0,4);
stringenddot=Ary[i].substring(Ary[i].lastIndexOf('.')+1,Ary[i].length);
stringlstslash=Ary[i].substring(Ary[i].lastIndexOf('/')+1,Ary[i].length);
if (stringstart=='http'&&(stringenddot=='htm'¦¦stringenddot=='html'¦¦stringenddot=='xml'¦¦stringenddot=='xml'¦¦stringlstslash ==' ')){
alert ('string'+i+' conforms');
}
}
}
//-->
</script>

2.

<script type="text/javascript"><!--
function stripurlout(tmpRUL){
var fullURL = window.self.location.href;
var splitURL = fullURL.replace(/^(http:\/\/)?(www\.)?/, "").split("/");
var tmpURL = "";
for(var i = 1; i < splitURL.length; i++){
tmpURL += splitURL[i]+"/";
};
tmpURL = tmpURL.replace(/\/$/, "");
alert("your folder location is:"+tmpURL);
//-->
}
</script>

3.

<script language="JavaScript">
<!--
// Set the varible g_url to nothing
var g_url = "";

// Split the document url by the /
var split_url=document.URL.split('/');

// Loop thorough the splits starting after http://
// and ending on the parent directory of the current document
for (var i=2; i<split_url.length-1; i++)
{
// Start rebuilding the url to link each level
g_url = g_url + split_url[i] +"/";
document.write("<a href='http://" + g_url +"'>" + split_url[i] + "</a>");

// Check to see if the last directory was written to avoid
// and extra > after the parent directory of the current page
if (i!= split_url.length-2)
{ document.write(" > "); }

// end loop
}
</script>

4.

<script type="text/javascript">
<!--

function stripout(s){
s = 'http://www.google.com/search?hl=pt-PT&lr=&q=related:www.ict4lt.org/en/en_mod3-4.htm';
n= (/http:\/\/www.+(www.+$)/i.test (s)? RegExp.$1 : 'not found')
}
</script>

5.
<script>
function splitURL(urlStr)
{ //EDIT: One more line in case URL string has back slashes
urlStr = urlStr.replace(/\\/g,'/');
res = /^(.*):\/\/([^\/]+)(\/?.*)\/(.+)\.(\w*)$/.exec(urlStr);
if(res)
{ urlBits = new Object();
urlBits.protocol = res[1];
urlBits.domain = res[2];
urlBits.path = res[3];
urlBits.file = res[4];
urlBits.extension = res[5];
return urlBits;
}
return null;
}
</script>

6.

<script type="text/javascript">

function URL (s) {
/^((\w+\/*)?((\w+\.)?\w+\.\w+)(\d+))?(\/[^\?#]+)?(\?[^#]+)?(#.+)?/.exec (s);-

// tHERE IS A PROBLEM IN THE ABOVE LINE!

this.protocol = RegExp.$2 ¦¦ 'http:';
this.host = RegExp.$5? RegExp.$3 + ':' + RegExp.$6 : RegExp.$5;
this.hostname = RegExp.$3;
this.port = RegExp.$6;
this.pathname = RegExp.$7;
this.search = RegExp.$8;
this.hash = RegExp.$9;

this.href= RegExp.$2? s : this.protocol + '//' + s;
if (!this.pathname &&!/\/$/.test (this.href)) this.href = this.href + '/';

}

url = new URL ('www.site/folder/index.htm');
alert (url.pathname);
// -->
</script>

Thank you very much in advance

g,

Bernard Marx

8:34 pm on May 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



function splitURL(s)
{
s.match( /^([^\/]+\/\/)?([^\/]+)([^\?#]+)(.*$)/ );
var tail = RegExp.$4, temp;

return {
protocol: RegExp.$1,
hostname: RegExp.$2,
pathname: RegExp.$3,
search : tail && (temp=tail.match(/\?[^#]*/)) && temp[0],
hash : tail && (temp=tail.match(/#[^\?]*/)) && temp[0]
};
}

url = splitURL('http://www.site.com/folder/index.htm#hashval?searchval=doo');
alert(url.search);

gaucho

9:16 pm on May 14, 2005 (gmt 0)

10+ Year Member



Thank you.

Sorry by my ignorance, but it is working for split out all URLS from a textarea?

Thanks

G.

Bernard Marx

10:49 pm on May 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No. None of the functions do that. They are for splitting URLs into parts.

gaucho

2:43 am on May 15, 2005 (gmt 0)

10+ Year Member



Thank you.
Do you know a function for split out all urls found at a textarea?

Many thanks for your patience

G

Bernard Marx

9:38 am on May 15, 2005 (gmt 0)

gaucho

9:51 am on May 15, 2005 (gmt 0)

10+ Year Member



Your script makes sense, but there is a long way until running! After 2 or 3 working pehaps I will be able to to running this function!

<script>
function spliturl(str){
str = 'boo haa href="http://www.foo.com/doo.htm" blah blah \n'
+ 'href = "http://www.foo.com.faa/daa.asp" dooo daah';
reg = /http[^"]+(asp¦[s]?htm[l]?¦xml)"/g
matches = str.match(reg)

alert(matches) // remove trailing quotemark yourself
}

</script>

Thanks

G.

Bernard Marx

12:37 pm on May 15, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm sorry. I don't understand what you are saying.

gaucho

2:06 pm on May 15, 2005 (gmt 0)

10+ Year Member



Ok. I did the following with your function:

BUT IT NOR WORKS! It gives only a message: NULL

html>
<head>
<title>Untitled</title>
<script type="text/javascript">
function narrow2() {
<!--
s = document.getElementById('txt').value;
alert (/http:\/\/www.+(www.+$)/i.test (s)? RegExp.$1:)

// -->
}
</script>
</head>

<body>

<form>
<textarea cols=140 rows=10 name="txt">
[google.com...]
[essex.ac.uk...]
[translate.google.com...]
</textarea>
<input type="button" onclick="narrow2()">
</form>

</body>
</html>

Thank you again.

JL