-2

I have some code that I've inherited in jquery in an asp.net MVC project. I'm not exactly sure of the specifics of what it's doing are, but in general it's redirecting to another page. It works fine in Chrome, but when I run it in IE 11, I get an error. Can someone help me?

The issue seems to be in using the @Html.Raw method. In IE, it shows an error saying that there is an invalid character:

JavaScript critical error at line 26, column 37 in http://localhost:1436/Scripts/javascript/Timeout.js\n\nSCRIPT1014: Invalid character

If I remove the @ symbol, I get a message saying that HTML is undefined.

I'm not sure what the new URI part is about, so I'm hesitant to remove the HTML.Raw method. How do I use HTML.Raw in IE?

Alternatively, how can I rewrite this command while still keeping whatever the URI part is doing?

Here's the jquery function:

 //Current time is greater than the expiry time
if (Date.parse(currentTime) > timeForExpiry) {
    alert("Session expired. You will be redirected to welcome page");
    window.applicationBaseUrl = @Html.Raw(HttpUtility.JavaScriptStringEncode(
    new Uri(
            new Uri(this.Context.Request.Url.GetLeftPart(UriPartial.Authority)),
            Url.Content("~/")
    ).ToString(), true))
    window.location = window.applicationBaseUrl + "Admin/Logout";
}
ataravati
  • 8,891
  • 9
  • 57
  • 89
boilers222
  • 1,901
  • 7
  • 33
  • 71
  • That's some sort of server-side thing. It's not plain JavaScript. – Pointy Dec 17 '15 at 15:41
  • @Html.Raw renders HTML that's sent to the browser. Right click the page that's erroring and "View Source". See what the generated code looks like, that will help you solve this. Post back the generated code if you are still stuck! – spozun Dec 17 '15 at 15:46
  • Looks like you have a problem in `Timeout.js`. Are you saying your jquery function in the question is in Timeout.js ? – freedomn-m Dec 17 '15 at 15:47
  • It looks like you're trying to use Razor syntax in a js file, which isn't going to be interpreted like you want. The text will be rendered to the output literally as written. Try assigning `window.applicationBaseUrl` in a cshtml view file, and only accessing it in this script. – Jason P Dec 17 '15 at 15:48
  • FYI you don't need `GetLeftPart` and `Url.Content("~/")` - just the url.content will give you what you need: `window.applicationBaseUrl = '@Url.Content("~")'` – freedomn-m Dec 17 '15 at 15:53

2 Answers2

2

You need to enclose your application url in quotes (otherwise it will think you are trying to use a js variable). Try:

window.applicationBaseUrl = '@Html.Raw(HttpUtility.JavaScriptStringEncode(
new Uri(
        new Uri(this.Context.Request.Url.GetLeftPart(UriPartial.Authority)),
        Url.Content("~/")
).ToString(), true))'

Not sure which part of the urls you are trying to get but it looks pretty complicated - I just usually use the following:

var baseUrl = '@Url.Content("~/")';   // gets the url from the application
var fullUrl = '@string.Format("{0}://{1}{2}", Request.Url.Scheme, Request.Url.Host, Url.Content("~/"))';  // gets the url with application and host

I should also note that this should be within script tags on your razor template and not in an external script file. If it is within an external script file then just add script tags to your razor before the file is included, declare a global like above and then use that global within external file

Pete
  • 57,112
  • 28
  • 117
  • 166
  • 1
    Just a note: Using url.scheme+url.host will not work if there's more to the left part (ie port and the obsolete login+pwd), specifically port is not included in `Request.Url.Scheme` - that's why you use `..GetLeftPart` – freedomn-m Dec 17 '15 at 15:54
  • @freedomn-m ah good to know, I've never had the need for adding a port to my scheme which is probably why I've been ok with the above – Pete Dec 17 '15 at 15:59
  • It get's pretty messy tbh - cause if you don't have one, do you have an `if` and check for it or not? etc etc more info: http://stackoverflow.com/questions/21640/net-get-protocol-host-and-port – freedomn-m Dec 17 '15 at 16:07
  • I couldn't get this to work for me (putting it in quotes). – boilers222 Dec 23 '15 at 20:47
0

I was able to get this to work just using a single command:

window.location = "Logout";

Little hesitant to use only this since it doesn't mention the URI stuff that the original code had. If anyone has any ideas how to improve this, please let me know.

boilers222
  • 1,901
  • 7
  • 33
  • 71