Response.write("<script>var _sessionID=" + Session.ID + ";</script>");
As long as the above comes before any JavaScript code that attempts to use the variable _sessionID (or the code that uses the var is inside a document ready function) the value _sessionID will be available globally throughout the script.window.addEventListener('load', function() {
if (_sessionID == 1) {
showAdminMenu();
}
else {
showUserMenu();
}
});
<script>
document.addEventListener('load', alert('Document Load through document.addEventListener()'));
window.addEventListener('load', alert('Document Load through window.addEventListener()'));
</script>
Working sample hereif (_sessionID == '1') {
src = "menu_Internal.js";
}
else {
src = "menu_External.js";
}
It looks like you are trying to dynamically set the src of a JavaScript file to import - unless you are going to employ one of the techniques described in my earlier post (which you alluded to you were not going to try) to dynamically load a script. I looked up the error and changed the HTML code to use attachEvent instead.Then you must be using IE8 as any real browser supports addEventListener. You might want to consider using the jQuery library or similar that will give you access to code that is cross platform.
It compiles and runs, but now I don't get any menu. Â (menu_Internal.js is still there.)You won't because
src = "menu_Internal.js";
Does nothing but assign a string to the variable src - JavaScript does not know what to do with src - it has no bearing on the loading of scripts. As discussed in earlier posts your Option 1 is not going to work unless you use one of the 3 dynamic script loading techniques I mentioned earlier.'Dynamically creating a <script> element and appending it to the body'.I did not provide references because you agreed you were going with option 2
So in my Login code I say: Â Response.Write("<script>vaYes - but you need to change 1 to whatever value you are going to use in your script AND the result of the above must be to write the <script> ...</script> to your HTML - if it is not doing that there is no way the JavaScript on the page will pick it up.r _sessionID=" + '1' + ";</script>");
(I am guess that this code put's the value of '1' into _sessionID, correct?)
<script>var _sessionID='external';</script>
Or (depending on the state of sessionID - again change to suite your app requirements)<script>var _sessionID='internal';</script>
When you do a view source in the browser you must see the above line somewhere in the code - if you are not - then your output (Response.write) is not firing or not firing correctly.(My assumption here is that this code picks up _sessionID ("1") and decides which menu code to run (internal or external))Yes - you are writing JavaScript into your output that contains state information from your server code so that the JavaScript code on the page can access it.
<script>var _sessionID='internal';</script>
However if the state indicates external you write the following to the output file<script>var _sessionID='external';</script>
That is done so now you type into the file the following<script language="JavaScript" type="text/javascript">
window.attachEvent('load', function () {
if (_sessionID == '1') {
src = "menu.js";
}
else {
src = "menu.js";
}
});
</script>
Now you are done creating the file so you save it and send it to the browser. You have put all the data into the file that the page needs in order to render correctly. Now just take yourself out of the picture and give the task you have just performed (creating the file) to the C# code - it is the same thing.
Please disregard the  src = "menu.js"; code - I cut and pasted the wrong iteration of what I am working on.  This code actually saysThe string is irrelevant - the method is wrong.
So we're back to square one.How so - I have given you the process you need to follow
<!-- THIS LINE IS OUTPUT FROM YOUR C# CODE AND CHANGES 'internal' / 'external'
DEPENDING ON THE STATE OF THE SERVER sessionID -->
<script>var _sessionID='internal';</script>
<!-- THIS CODE IS STATIC IT CAN JUST BE INCLUDED -->
<script>
function RenderInternalMenu()
{
console.log('Output internal menu from here');
}
function RenderExternalMenu()
{
console.log('Output external menu from here');
}
if (document.addEventListener) {
document.addEventListener('load', onDocLoad());
}
else {
document.attachEvent('load', onDocLoad());
}
function onDocLoad()
{
if (_sessionID == 'internal') {
RenderInternalMenu();
}
else {
RenderExternalMenu();
}
}
</script>
function onDocLoad()
{
if (_sessionID == 'internal') {
src = 'menu.Internal.js';
}
else {
src = 'menu.External.js';
}
var sc = document.createElement('script');
sc.src = src;
document.getElementsByTagName("head")[0].appendChild(sc);
}
Another option is instead of writing out the var _sessionID you instead do this (preferably in the <head> section of your target document)if (sessionID == 'internal) {
Response.write("<script src=\"menu.Internal.js\"></script>");
}
else {
Response.write("<script src=\"menu.External.js\"></script>");
}
if (Session["UserType"].ToString() == "INTERNAL")
{
Response.Write("<script src=\"menu_Internal.js\"></script>");
}
else
{
Response.Write("<script src=\"menu_External.js\"></script>");
}
Context is important - simply writing out the above is no good unless it is done in the right place<script language="JavaScript" type="text/javascript">
if (sessionID == 'internal') {
Response.Write("<script src=\"menu_Internal.js\"></script>");
}
else {
Response.Write("<script src=\"menu_External.js\"></script>");
}
</script>
<script src="menu_Internal.js"></script>
OR<script src="menu_External.js"></script>
So instead of seeing the first block of code we should be seeing one of the above to lines.using System;
namespace t2286
{
public partial class _Default : System.Web.UI.Page
{
public int SessionId;
protected void Page_Load(object sender, EventArgs e)
{
Random rnd = new Random();
SessionId = rnd.Next(1, 3);
}
}
}
The ASPX page<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="t2286._Default" %>
<!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 runat="server">
<%
if (SessionId == 1)
Response.Write("<script src=\"http://code.jquery.com/jquery.js\"></script>");
else
Response.Write("<script src=\"https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.j\"></script>");
%>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<%=SessionId %>
</div>
</form>
</body>
</html>
You can see it working here if (MenuId == "INTERNAL")
Response.Write("<script src=menu.js type="text/javascript"></script>;
else
Response.Write("<script src=menu_External.js type="text/javascript"></script>;
Is there any way to get a variable set in C# from the Login page into the menu.js menu program? ÂYes but it involves getting hold of MenuID which you are having trouble with. If you can access it to output it to the script then you can access it to do the logic test.
What you can do is emit JavaScript code that includes JS variables that are set to values from the C# session for instance
(For illustration purposes only - adapt for your code)
Open in new window
Now when your page is ready there will be a global _sessionID variable that your JavaScript code will be able to access.Option 1 won't really work - both scripts will be included. You would need to load the script dynamically using one of three methods
1. jQuery $.getScript
2. Document write (providing it is in the body of the document and does not run after document ready)
3. Dynamically creating a <script> element and appending it to the body
Neither of the strategies, in my view, is optimal but an alternative would require a radical change from your existing architecture which I don't believe is in your best interest right now. So working with what you have - if you have two distinct scenarios for menus based on a variable then these options are good enough.