B.Othman
B.Othman

Reputation: 13

How to retrieve item from another localStorage Domain?

I'm working on a JAVA web application which plays the role of a search engine. Its backend is developed with AngularJS. And I have a small PHP code which tries to send a data from an input text to the JAVA web app via an iframe. the data is stocked in a localStorage.

Here is below the PHP Code :

index.php

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

</head>
<body>
 <script type="text/javascript">
 	
 function redirect(){

 	var toSearch = document.getElementById('wordToSearch').innerHTML;
 	console.log("toSearch : ", toSearch);
 	localStorage.setItem('wordToSearch', 'bdd');
 	window.location.href = "http://localhost:8050/searchEngine/#/search";
 	
 }

 </script>
 <form action="http://localhost:8050/searchEngine/#/search">
 	<input type="text" name="word" id="wordToSearch">
    <input type="submit" value="Search"  onclick="redirect()">
 </form>
</body>
</html>

Precisely, I'm trying to retrieve the "wordToSearch" item value stored within the localStorage

Here is below the code where i add my iFrame to my application :

<iframe src='http://localhost:8040/test/index.php'></iframe>

This is the result i get in the developer tools :

Developer Tools Result

Can anyone help me please ?

Upvotes: 0

Views: 2561

Answers (2)

Arif Ibrahim
Arif Ibrahim

Reputation: 61

You can do it using cookie, set value to http://localhost:8050 and get value from http://localhost:8040. Visit https://developer.mozilla.org/en-US/docs/Web/API/Document/cookie

Upvotes: 1

T.J. Crowder
T.J. Crowder

Reputation: 1075029

So TL;DR, you're saying you want JavaScript code running on a page whose origin is http://localhost:8050 to access the localStorage for origin http://localhost:8040 (or vice-versa)?

You can't do that. Web storage is specific to the origin, which includes the port.

Upvotes: 4

Related Questions