Hey,

I was wondering if anyone could show me a little javascript. I've never used it before and am unfamiliar with how to reference outside API's.

What I'm trying to do is read a string in from a website/Web API. So..:

HTML with javascript document:

<script type="text/javascript">
var wantedString = http://foo.bar/prototype?misfunction&itemId=1
</script>

Web API or http://foo.bar/prototype?misfunction&itemId=1 accessed in a web browser would show:
foo bar text

I need help getting the text from the web API with the javascript document.

Thanks!

Recommended Answers

All 5 Replies

You might want to try using jQuery as a framework, it is really simple and clean, although it will add a little extra code to your page.

If you are just looking for the URL, you could use the window variable's location property:

var wantedString = window.location;

If you want to separate out the variables, you could try this tutorial.

Thanks joehms22 but I was asking to access an outside page. I stumbled upon XMLHttpRequest however it doesn't appear to be working in Firefox, Chrome, etc... The only browser I seem to be able to get a response from is Internet Explorer.

Any Ideas?

var request;
var itemId=1;
try{
	request = new XMLHttpRequest();
} catch(e) {
	try {
		request = new ActiveXObject("Microsoft.XMLHTTP");
	} catch(e) {
		alert("Error creating XMLHttpRequest \n" + e);
	}
}
request.open("GET", "http://foo.bar/prototype?misfunction&itemId=" + itemId, true);
request.onreadystatechange = function(){
	if (request.readyState == 4 ){
		alert(request.responseText);						
	}
};
request.send(null);

IE returns

parameters given:
parameter: itemId, value:1
parameter: misfunction, value:

results found
"the text i'm looking for"

Oh, so you want to fetch text from a page and use it? The string being returned from the page should be properly formatted for JS too, as I believe they use the same escape sequences.

If you want to look at debugging info on Firefox you can go to the Firefox menu > web developer > web console, then reload the page and it should give you some hints as to what is going wrong.

The easiest way I know to do an AJAX request, again is using jQuery's get function:http://api.jquery.com/jQuery.get/ This does things in the best way for whatever browser it is running on.

Try pointing the URL to the same page that generates it as a test, when I did that, things worked just fine under Firefox. Remember that Cross Domain requests are not allowed in AJAX or any Javascript for that matter due to security concerns (i.e. scripts from www.google.com can't connect to www.yahoo.com). Some older versions of IE may allow this, and/or may allow it if you just open it up from your desktop as that really isn't a domain.

This is the way it worked on Firefox/Chrome for me:

<html>
<head>
<script type='text/javascript'>
	var request;
	var itemId=1;
	try{
		request = new XMLHttpRequest();
	} catch(e) {
		try {
			request = new ActiveXObject("Microsoft.XMLHTTP");
		} catch(e) {
			alert("Error creating XMLHttpRequest \n" + e);
		}
	}
	request.open("GET", "file:///home/joseph/Desktop/js.html", true);
	request.onreadystatechange = function(){
		if (request.readyState == 4 ){
			alert(request.responseText);						
		}
	};
	request.send(); // The null isn't strictly necessary here as any params left out are automatically null
</script>
</head>

<body>
<h1>hello world</h1>
</body>
</html>

- Joe

Thanks I think this will work.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.