CrossBrowdy - Examples

Networking

Fetch

This is an example managing Fetch:

index.html:

<!DOCTYPE html>
<html>
	<head>
		<meta http-equiv="content-type" content="text/html; charset=utf-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no" />
		<title>Networking: Fetch - Example</title>
		<!-- Loads the needed CSS files: -->
		<link rel="stylesheet" type="text/css" href="main.css" />
		<!-- Loads CrossBrowdy.js (main file): -->
		<script src="CrossBrowdy/CrossBrowdy.js" type="text/javascript" language="javascript"></script><!-- "type" and "language" parameters for legacy clients. -->
		<!-- Loads the other needed script files: -->
		<script src="main.js" type="text/javascript" language="javascript"></script>
	</head>
	<body>
		Content got ('test_fetch_text.txt' file): <span id="content"></span>
		<br />
		<!-- The "CB_console" element will be used automatically in the case that the client does not support console: -->
		<div id="CB_console" style="visibility:hidden; overflow:scroll;">
			<span style="font-weight:bold;">Console:</span><br />
		</div>
		<div id="crossbrowdy_info"><a href="/guides#examples" target="_blank">CrossBrowdy.com</a> example</div>
	</body>
</html>

main.css:

body { background-color:#aaddee; word-wrap:break-word; }
#crossbrowdy_info { position:fixed; bottom:2px; right:2px; }
#crossbrowdy_info a { color:#00aadd; }
#crossbrowdy_info a:hover { color:#0033aa; }
span { color:#aa0000; }

main.js:

CB_init(main); //It will call the "main" function when ready.


//This function will be called when CrossBrowdy is ready:
function main()
{
	//Gets the content through Fetch:
	CB_Net.Fetch.get("data/test_fetch_text.txt").then
	(
		//Treats the response as text:
		function(response) { return response.text(); }
	).then
	(
		//Displays the content received:
		function(content)
		{
			CB_console("Content received: " + content);
			CB_Elements.insertContentById("content", content);
		}
	)["catch"] //This way we prevent errors because of using "catch" since it is a reserved word.
	(
		//Displays the error:
		function(error)
		{
			CB_console("Error:");
			CB_console(error);
			CB_Elements.insertContentById("content", "ERROR: " + content);
		}
	);	
}
Additional file used (inside the "data" folder): test_fetch_text.txt.

Try this example

You can check the Guides & Tutorials category as well as the API documentation in the case you need more information.

Go back to Guides & Tutorials

« PrevReturnNext »
CrossBrowdy by Joan Alba Maldonado