I am working on a web app that can display data in two drastically different formats. To do this, I am using a master page with two different content pages for the differing views. I am using .svc files to do AJAX style server requests. I would like to be able to do a service call from the master pages javascript, then run the appropriate onSuccess javascript method (which would ideally lie in another .js file) to display the data based on which content page I am in. I am guessing this would be done with some kind of function delegate, but I am new to web development and not sure how to do this. Any help would be greatly appreciated.
Asked
Active
Viewed 208 times
0
-
if you are rendering two separate browser webviews (like separate tabs, or iframes), i think there is no way to do this. usually the solution is to build a single-page app with angular or something so you can separate the logic that controls the two independent pieces but they can still share data. That said: Im not sure what your document structure is, and I might be wrong, so you should take a look at [DOM events](https://developer.mozilla.org/en-US/docs/Web/API/Event/Event), and try broadcasting an event from your master `document` DOM object and receiving it in a child DOM object – Plato Jul 06 '15 at 20:02
-
As long as you're loading everything from one top-level html file, you can certainly put the second function in a second javascript file and call it from the first javascript file. (But beware, the second script won't be loaded when the first script runs - use [`DOMContentLoaded`](https://developer.mozilla.org/en-US/docs/Web/Events/DOMContentLoaded) to wait if you need to.) – Plato Jul 06 '15 at 20:06
2 Answers
0
If you can do everything on one page, your HTML will probably look something like this:
<!DOCTYPE html>
<html>
<head>
<script src="masterScript.js"></script>
<script src="module/childScript.js"></script>
<script>
console.log(globalVariableFromMasterScript)
doSomethingFromChildScript(globalVariableFromMasterScript)
</script>
</head>
<body>
<p>This is the html page</p>
</body>
</html>

Plato
- 10,812
- 2
- 41
- 61
0
This was a stupid question... Just attach different js files to the content pages. Give the functions the same name and it works fine.

unbootabru
- 347
- 1
- 3
- 9