Truemag

  • Categories
    • Tips And Tricks
    • Internet
    • PHP
    • Javascript
    • CSharp
    • SQL Server
    • Linux
  • Lastest Videos
  • Our Demos
  • About
  • Contact
  • Home
  • Write With Us
  • Job Request
Home Javascript JavaScript Detect Protocol (HTTP/ HTTPS) To Load Or Handle Accordingly

JavaScript Detect Protocol (HTTP/ HTTPS) To Load Or Handle Accordingly

If you site’s running on both HTTP and HTTPS, we will need to determine whether the HTTP protocol for the requested page is either secure (HTTPS) or standard (HTTP) to load/write the appropriate external assets files or handle process correspondingly.

1. JavaScript Detect Protocol To Load External File

A piece of JavaScript below gives an example to detect the protocol of current page to load the appropriate Google Analytics js file:

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>

<script type="text/javascript"> var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www."); document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E")); </script>

2. JavaScript Detect Protocol To Handle Process Accordingly

JavaScript code below detects protocol to respond accordingly.

<script type="text/javascript">
	if("https:" == document.location.protocol)
	{
		document.write("<h2>The page is loaded via HTTPS</h2>");
 
		// your code for HTTPS ...
	}
	else
	{
		document.write("<h2>The page is loaded via HTTP</h2>");
 
		// your code for HTTP ...
	}
</script>

<script type="text/javascript"> if("https:" == document.location.protocol) { document.write("<h2>The page is loaded via HTTPS</h2>"); // your code for HTTPS ... } else { document.write("<h2>The page is loaded via HTTP</h2>"); // your code for HTTP ... } </script>

3. Demonstration

+ HTTPS: https://4rapiddev.com/demo/HTML/load_http_https.html

+ HTTP: http://4rapiddev.com/demo/HTML/load_http_https.html

Apr 21, 2012Hoan Huynh
Encrypt With MD5, SHA Or SHA1 In MS SQL ServerGet WordPress Most Popular Posts By Total Comments
You Might Also Like:
  • Get Image Width Height With JQuery And JavaScript
  • How To Track Website With Multiple Google Analytisc Accounts
  • Create Pie Chart With HTML & JavaScript
  • Javascript Problem Set focus textbox on Firefox
  • HTTP/ HTTPS Document, CGI-BIN And Access/ Error Logs On cPanel, Plesk And VirtualMin
  • Jquery checkbox checked
  • Create Stacked Column Chart With HTML & JavaScript
  • Auto Rotate Web Page Title With JavaScript
  • Detect Mobile Devices (iPhone, Android) With JavaScript, PHP And .Net (CSharp)
  • Auto Load YouTube Video As A Popup With JQuery FancyBox
Hoan Huynh

Hoan Huynh is the founder and head of 4rapiddev.com. Reach him at [email protected]

8 years ago JavascriptHTTP, HTTPS, Protocol742
0
GooglePlus
0
Facebook
0
Twitter
0
Digg
0
Delicious
0
Stumbleupon
0
Linkedin
0
Pinterest
Most Viewed
PHP Download Image Or File From URL
22,164 views
Notepad Plus Plus Compare Plugin
How To Install Compare Text Plugin In Notepad Plus Plus
20,041 views
Microsoft SQL Server 2008 Attach Remove Log
Delete, Shrink, Eliminate Transaction Log .LDF File
15,821 views
JQuery Allow only numeric characters or only alphabet characters in textbox
13,296 views
C# Read Json From URL And Parse/Deserialize Json
9,790 views
4 Rapid Development is a central page that is targeted at newbie and professional programmers, database administrators, system admin, web masters and bloggers.
Recent Posts
  • Installment Loans – Making Sense of Online Software
  • Apple Pay Casino Canada
  • Casinos austria
  • Essay For Sale – How To Write A Superb One That Will Sell
  • Free Online Photo Editor
Categories
  • CSharp (45)
  • Facebook Graph API (19)
  • Google API (7)
  • Internet (87)
  • iPhone XCode (8)
  • Javascript (35)
  • Linux (28)
  • MySQL (16)
  • PHP (84)
  • Problem Issue Error (29)
  • Resources (32)
  • SQL Server (25)
  • Timeline (5)
  • Tips And Tricks (141)
  • Uncategorized (103)
Recommended
  • Custom Software Development Company
  • Online Useful Tools
  • Premium Themes
  • VPS
2014 © 4 Rapid Development