PageSourceSearch

https://cuhk.edu.hk/english/js/common.js?202308

js cuhk.edu.hk collected 2026-10-01 07:45:45 UTC 18,695 bytes, 504 lines download raw bytes

1var objPrintWindow = null;
2
3var setBlockHeightList = [];
4var view_mode_list = ["lg", "md", "sm", "xs"];
5var current_view_mode = "";
6
7function detectXS() {
8     return (window.innerWidth < 768);
9}
10
11//Convert into https mode
12if(location.protocol == "http:") {
13	var strResultUrl = "";
14	if(location.port + "" == "88" ) {
15		strResultUrl = location.href.replace(/^http:([^:]+):\d+\//, "https:$1:4438/")
16	} else if(location.port + "" == "" ) {
17		strResultUrl = location.href.replace(/^http:/, "https:")
18	}	
19	console.log("converted strResultUrl: " + strResultUrl);	
20	location.href = strResultUrl;
21}
22
23
24//If it is loaded from translate server, change to www.cuhk.edu.hk
25if(/^translate\d?\.itsc\.cuhk\.edu\.hk$/.test(document.location.hostname)) {
26	location.href = location.href.replace(/translate\d?\.itsc\.cuhk\.edu\.hk\/[^\/]+/, "");
27}
28
29
30$( document ).ready(function() {
31	
32	//Reassign Branch-title image
33	if ($("#branch-title >img").length > 0) {
34		var strBranchTitleImgFile = $("#branch-title >img").attr("src");
35		if (strBranchTitleImgFile.indexOf("?") < 0) {
36			$("#branch-title >img").attr("src", strBranchTitleImgFile + "?20150828");
37		}
38	}
39	
40	// Nav Menu roll over 			
41	$('.dropdown').each(function(index, element) {
42		
43		$(element).children(' .dropdown-toggle').mouseenter(function () {
44			if (!detectXS()) {
45				$(element).children(' .dropdown-toggle').dropdown('toggle');
46			}
47		});
48						
49		$(element).children(' .dropdown-menu').mouseleave(function () {
50			if (!detectXS()) {			
51				$(element).children(' .dropdown-toggle').dropdown('toggle');
52				$(element).children(' .dropdown-toggle').trigger('blur');				
53			}
54		})
55										
56	});
57	
58	
59	//Clickable on parent item in nav menu
60	$("#navbar li.dropdown > a").click(function(e) {
61		if (Modernizr.touch == false) {
62			if (typeof e.which != "undefined" && e.which != 37 && e.which != 38 && e.which != 39 && e.which != 40 ) { 
63				//Not up, down, left , right arrow keys or undefined 
64				location.href = $(this).attr("href");
65			}
66		}
67		
68	});
69
70	//Handle go-to-content link
71	$("a#to-content-pane").click(function(e) {
72		e.preventDefault();
73		
74		if ($("#content nav#vert-nav a:first").length > 0) {
75			$("#content nav#vert-nav a").first().focus();
76		} else if ($("#content a:first").length > 0) {
77			$("#content a").first().focus();
78		} else if ($("#feature-container figure.item.active a:first").length > 0) {
79			$("#feature-container figure.item.active a").first().focus();
80		} 
81		return false;
82	});
83	
84	
85	
86	//Change Language
87	$("#linkTC, #linkSC, #linkTC2, #linkSC2").click(function(e) {
88		e.preventDefault();
89		
90		var strUrl = location.href;
91		
92		if(/^translate\d?\.itsc\.cuhk\.edu\.hk$/.test(document.location.hostname)) {
93			strUrl = strUrl.replace(/translate\d?\.itsc\.cuhk\.edu\.hk\/[^\/]+/, "");
94		}
95
96		if (this.id.indexOf("TC") >= 0) {
97			location.href = strUrl.replace(/\/english\//, "/chinese/");
98		} else if (this.id.indexOf("SC") >= 0) {
99			location.href = strUrl.replace(location.hostname, "translate.itsc.cuhk.edu.hk/uniTS/" + (location.hostname == "cuhk.edu.hk" ? "www.cuhk.edu.hk" : location.hostname ) ).replace(/\/english\//, "/chinese/");
100			//"http://translate.itsc.cuhk.edu.hk/uniTS/" + location.href.replace(/\/english\//, "/chinese/").replace('http://','');
101		}
102	});
103	
104	
105	// Font size changer
106	var fontSizes = [12, 14, 16];
107	$('#text-size a').click(function(e) {
108		$('#text-size a').removeClass('active');
109		var iIndex = $(this).index() - 1;
110		createCookie("fontsize", iIndex, 365);
111		$('body').css('font-size', fontSizes[iIndex] +  'px');
112		$(this).addClass('active');
113		
114		//Making registered blocks same height
115		if (setBlockHeightList.length > 0) {
116			for (i in setBlockHeightList) {
117				if (current_view_mode == "xs" && $(setBlockHeightList[i][0]).closest(".col-xs-12.yellow, .col-xs-12.alumni-col").length > 0 )  {
118					setBlockHeightList[i].clearBlockHeight();
119				} else {
120					setBlockHeightList[i].setBlockHeight();
121				}
122			}
123		}
124	})
125	
126	//get font cookie
127	var fontsize_index = readCookie("fontsize");
128	if (/^[012]$/.test(fontsize_index)) {
129		//0 - small, 1 - medium, 2 - large
130		$('#text-size a:eq(' + fontsize_index + ')').click();
131	}
132	
133	// Cloning top links/shortcuts and prospective students/students/staff/alumni/media/visitors for mobile version
134	// prospective students/students/staff/alumni/media/visitors
135	$("li#top_buttons_mobile").html("");
136	$toplinks_ul = $("<ul></ul>").attr("id", "top_link_2m");
137	$("#top_link_2 >ul >li:has(a)").each(function(e) {
138		var $li = $(this).clone().addClass("col-xs-4 centre-parent");
139		$(">a", $li).wrapInner($("<span></span>").addClass("centre-child"));
140		$li.appendTo($toplinks_ul);
141	});
142	$toplinks_ul.appendTo("li#top_buttons_mobile");
143	
144	// Top Links/Shortcuts
145	$shortcuts_mobile_wrapper = $("li#shortcuts-dropdown-m");
146	$pre_shortcuts_mobile = $("<ul></ul>").addClass("pre-dropdown-menu"); //.attr("role", "menu");
147	$shortcuts_mobile = $("<ul></ul>").addClass("dropdown-menu"); //.attr("role", "menu");
148	// Top Links
149	$("#top_link_1 > a").each(function() {
150		var $new_li = $("<li></li>");
151		$(this).clone().appendTo($new_li);
152		$new_li.addClass("top-link-item");
153		$new_li.appendTo($pre_shortcuts_mobile);
154	});
155	
156	//Add Divider
157	$("#shortcuts-dropdown-m >a.dropdown-toggle >span").remove();
158
159	var $a = $("<a></a>").attr("href", "javascript:;").addClass("clickmore").addClass("shortcutclickmore").text("Shortcuts");
160	
161	var $li = $("<li></li>").addClass("top-link-item");
162	
163	$a.appendTo($li);
164	$li.appendTo($pre_shortcuts_mobile);
165	$("#shortcuts-dropdown-m > a:first").remove();
166	
167	$pre_shortcuts_mobile.appendTo($shortcuts_mobile_wrapper);
168	//$shortcuts_mobile_wrapper.addClass("open");
169	//$("<li class=\"divider\"></li>").appendTo($pre_shortcuts_mobile);
170	//$(".dropdown-toggle").addClass("opened");
171	
172	var bClickedByMoreLink = false;
173	
174	/*$("#shortcuts-dropdown-m.dropdown").on("shown.bs.dropdown hidden.bs.dropdown", function(event){	
175		var x = $(event.target); // Get the text of the element
176		console.log(x[0].tagName + " : " + event.type + " : " + event.relatedTarget + " : " + event.target);
177		var eventType = event.type;
178		
179		if (eventType == "hidden") {$("a.clickmore").show(); $("#shortcuts-dropdown-m ul.pre-dropdown-menu >li").addClass("top-link-item");} else {$("a.clickmore").hide(); $("#shortcuts-dropdown-m ul.pre-dropdown-menu >li").removeClass("top-link-item");}
180		event.stopPropagation();
181		return false;
182	});
183	
184	$("#shortcuts-dropdown-m.dropdown").on("show.bs.dropdown hide.bs.dropdown ", function(event){	
185		var x = $(event.target); // Get the text of the element
186		console.log("**********" + x[0].tagName + " : " + event.type + " : " + event.relatedTarget + " : " + event.target);
187		//var eventType = event.type;
188		
189		if (event.type == "hide" && bClickedByMoreLink) {
190			//It is expected that when a.clickmore is clicked, dropdown('toggle') is called (****) which triggers "show.bs.dropdown" event and then "shown.bs.dropdown" afterwards.
191			//One of the strange thing is that it then trigger "hide.bs.dropdown" event automatically.
192			//In order not to collapse the dropdown in "hide" event, the event needs to return false to stop from proceeding when variable "bClickedByMoreLink" is true (which means the event is caused by clicking the "More..." link 
193			bClickedByMoreLink = false;
193 //reset variable bClickedByMoreLink to false
194			return false;  //return false to stop from proceeding
195		} else {
196			return true;
197		}
198	});*/
199		
200	$(document).on("click", "a.clickmore", function(e) {
201		
202		$("#shortcuts-dropdown-m").toggleClass("open");
203		//$("li#shortcuts-dropdown-m.dropdown >a.dropdown-toggle").dropdown('toggle');
204		
205		bClickedByMoreLink = true;
206	});
207
208	//Shortcuts
209	$("#top_link_1 > div.dropdown > ul.dropdown-menu >li").each(function() {
210		$(this).clone().appendTo($shortcuts_mobile);
211	});
212	$shortcuts_mobile.appendTo($shortcuts_mobile_wrapper);
213	
214	//Social Channels
215	if ($("#cuhk-social-channels").length > 0) {
216		var $social_channels_bottom = $("#cuhk-social-channels").clone();
217		
218	//	$social_channels_bottom.removeAttr("id").addClass("visible-xs");
219		
220		$("img", $social_channels_bottom).each(function() {
221				var strImgSrc = $(this).attr("src");
222				$(this).attr("src", strImgSrc.replace("purple", "gray"))
223		});		
224		//$social_channels_bottom.prependTo("footer>nav.full-col");
225	}
226	
227	//Update vert menu
228	if ($('#vert-nav').length) {
229	
230		var updateVertNav = function() {
231			if (detectXS()) {
232				$('#vert-nav').insertAfter($('#main_content'));
233			} else {
234				$('#vert-nav').insertBefore($('#main_content'));
235			}				
236		}
237	
238		$(window).resize(updateVertNav);
239		$(window).load(updateVertNav);
240	
241	}else{
242		// no-vert-nav
243		$('#main_content').addClass('no-vert-nav');
244	}
245
246	//$(window).resize(updateVertNav);
247	//$(window).load(updateVertNav);
248
249	//Social Bookmarking
250	$(document).on("click", "#share a", function() {
251		//console.log(this.id);
252		var strID = this.id;
253		var strUrl = document.location.href.replace(/webserver\.cuhk\.edu\.hk:\d+/, "www.cuhk.edu.hk");
254		if (strID == "facebook") {
255			//window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(strUrl)+'&t='+encodeURIComponent(document.title),'_blank','width=930,height=470,left=50,top=50,toolbar=no,menubar=no,location=no,scrollbars=yes,status=yes,resizable=yes');
256			
257			//Update on 2016/7/14
258			FB.ui({
259			  method: 'share',
260			  mobile_iframe: true,
261			  href: strUrl,
262			  display: 'popup',
263			  mobile_iframe: true
264			}, function(response){});
265
266			bookmarkingLogging('Facebook');
267			
268		} else if (strID == "twitter") {
269			window.open('http://twitter.com/home?status='+encodeURIComponent(document.title)+'+'+encodeURIComponent(strUrl),'_blank','width=930,height=470,left=50,top=50,toolbar=no,menubar=no,location=no,scrollbars=yes,status=yes,resizable=yes');
270			bookmarkingLogging('Twitter');
271		} else if (strID == "google") {
272			
273			//window.open('http://www.google.com/bookmarks/mark?op=edit&bkmk='+encodeURIComponent(strUrl)+'&title='+encodeURIComponent(document.title)	,'_blank','width=930,height=470,left=50,top=50,toolbar=no,menubar=no,location=no,scrollbars=yes,status=yes,resizable=yes');
274			window.open('https://plus.google.com/share?url='+encodeURIComponent(strUrl) + '&hl=en_US' ,'_blank','width=930,height=470,left=50,top=50,toolbar=no,menubar=no,location=no,scrollbars=yes,status=yes,resizable=yes');
275
276			bookmarkingLogging('Google');
277		
278		} else if (strID == "baidu") {
279			window.open('http://cang.baidu.com/do/add?it='+encodeURIComponent(document.title.substring(0,76))+'&iu='+encodeURIComponent(strUrl)+'&fr=ien#nw=1','_blank','scrollbars=no,width=600,height=450,left=75,top=20,status=no,resizable=yes');
280			
281			bookmarkingLogging('Baidu');
282		} else if (strID == "qq") {
283			window.open('http://shuqian.qq.com/post?from=3&title='+encodeURIComponent(document.title)+'&uri='+encodeURIComponent(strUrl)+'&jumpback=2&noui=1','favit','width=930,height=470,left=50,top=50,toolbar=no,menubar=no,location=no,scrollbars=yes,status=yes,resizable=yes');
284
285			bookmarkingLogging('QQ');
286		} else if (strID == "weibo") {
287			window.open('http://service.weibo.com/share/share.php?url=' + encodeURIComponent(strUrl) + '&appkey=&title=' + encodeURIComponent(document.title) + '&pic=&ralateUid=&language=zh_hk', 'favit','width=930,height=470,left=50,top=50,toolbar=no,menubar=no,location=no,scrollbars=yes,status=yes,resizable=yes');
288
289			bookmarkingLogging('Weibo');
290		
291		} else if (strID == "tencent") {
292			window.open('http://share.v.t.qq.com/index.php?c=share&a=index&url=' + encodeURIComponent(strUrl) + '&appkey=801316217&title=' + encodeURIComponent(document.title) + '&pic=&line1=', 'favit','width=930,height=470,left=50,top=50,toolbar=no,menubar=no,location=no,scrollbars=yes,status=yes,resizable=yes');
293
294			bookmarkingLogging('Tencent');
295		
296		} else if (strID == "linkedin") {
297			window.open('https://www.linkedin.com/cws/share?url=' + encodeURIComponent(strUrl) + '&token=&isFramed=false&lang=en_US', 'favit','width=930,height=470,left=50,top=50,toolbar=no,menubar=no,location=no,scrollbars=yes,status=yes,resizable=yes');
298			
299			bookmarkingLogging('Linkedin');
300		}
301		
302	});
303	
304	function bookmarkingLogging(strProvider) {
305		var strTitle = document.title;
306		var strUrl = document.location.href;
307		
308        $.get('//apps.cuhk.edu.hk/cuhkwebsite/social-bookmarking-stat.as
308px', 
309			{'title': strTitle, 'provider':strProvider, 'url':strUrl}, 
310			function(data) {
311				if (/true/i.test(data.status)) { //If Status == "true"
312					//alert(data.reason);
313                } else {
314					//alert(data.reason);
315                }
316            }, 'jsonp');
317	}
318	
319	
320		//Print Friendly
321	$("a#btnPrint").click(function(event) {
322		event.preventDefault();
323		//
324		if (objPrintWindow != null) {
325			objPrintWindow.close();
326		}
327		
328		var iPageWidth = 790;
329		
330		if ($("div#main_content").length > 0) {
331			iPageWidth = $("div#main_content").width();
332		} else if ($("div#vc_main").length > 0) {
333			iPageWidth = $("div#vc_main").width();
334		}
335		
336		//objPrintWindow = window.open('','print_content','resizable=yes, scrollbars=yes');
337		objPrintWindow = window.open('','print_content','width=640,height=480');
338		var $printWrapper = $("<span/>");
339		var $printContent = $("<div/>").attr({"id":"main_content"});
340		//var strHtml = "<html xmlns=\"http://www.w3.org/1999/xhtml\"><head><title>[title]</title>[css]</head><body onload=\"window.print();\" style=\"background:none #ffffff; min-width:" + iPageWidth + "px;\">[content]</body></html>";
341		var strHtml = "<!DOCTYPE html>\r\n<html><head><title>[title]</title>[css]</head><body onload=\"window.print(); window.close();\" style=\"background:none #ffffff; min-width:" + iPageWidth + "px;\">[content]</body></html>";
342		var strLink = "";
343		
344		$("link[rel='stylesheet']").each(function() {
345			if (this.disabled == false) {
346				strLink += "<link rel='stylesheet' href='" + $(this).attr("href") + "'/>\r\n";
347			}
348		});
349		//div#main_content for CUHK website pages, div#vc_main for Governance pages
350		$printContent.html($("div#main_content, div#vc_main").html()).appendTo($printWrapper);
351		strHtml = strHtml.replace("[title]", document.title).replace("[css]", strLink).replace("[content]", $printWrapper.html());
352		
353		objPrintWindow.document.open();
354		objPrintWindow.document.write(strHtml);
355		objPrintWindow.document.close();
356		
357		//setTimeout(function(){objPrintWindow.close();},1000);
358		
359	});
360	
361	
362	// Phone number
363	if (/(iPhone|iPad|iPod)/i.test(navigator.userAgent) == false) {
364		$('#content td, #content p, #content span, #content li').each(function() {
365			var htmlText = $(this).html();
366				$(this).html(htmlText.replace(/\((\d{3})\)\s*(\d{4})\s?(\d{4})/g, '<a class="nowrap" href="tel:+$1$2$3">$&</a>')); 
367		});
368	}
369	
370	
371	// Detect screen height to make sure footer is at screen bottom
372	function updatePageMinHeight () {
373		var windowHeight = $(window).height();
374		var footerHeight = $('footer').height() + 50; //50 = padding top and bottom
375		$('#body-frame').css('min-height', windowHeight - footerHeight);
376	}
377	setInterval (updatePageMinHeight, 200);
378	
379	$(window).on("resize", function(e) {
380
381		var $test_block = $("<div></div>").appendTo("body");
382		
383		for(i in view_mode_list) {
384			$test_block.addClass("visible-" + view_mode_list[i]);
385			if (i>1) //Remove the previous class
386				$test_block.addClass("visible-" + view_mode_list[i]);
387			if ($test_block.is(":visible")) {
388				if (current_view_mode != view_mode_list[i]) {
389					current_view_mode = view_mode_list[i];
390				}
391				break;
392			}
393		}
394		
395		$test_block.remove();
396		
397		//Making registered blocks same height
398		if (setBlockHeightList.length > 0) {
399		
400			if (current_view_mode == "xs")  {
401				if ($(window).width() > 360) {
402					$(".col-xs-12.yellow").removeClass("col-xs-12").addClass("col-xs-6");
403				} else {
404					$(".col-xs-6.yellow").removeClass("col-xs-6").addClass("col-xs-12");
405				}
406			}
407
408			for (i in setBlockHeightList) {
409			
410				if (current_view_mode == "xs" && $(setBlockHeightList[i][0]).closest(".col-xs-12.yellow, .col-xs-12.alumni-col").length > 0 )  {
411					setBlockHeightList[i].clearBlockHeight();
412				} else {
413					setBlockHeightList[i].setBlockHeight();
414				}
415			}
416		}
417	});
418	
419	//CUHK Survey
420	var objDate = new Date();
421	var strDateTime = objDate.toLocaleString().replace(/[\W\s]+/g, "");
422	var strSurveyUrl = (location.hostname.indexOf("translate") >= 0 ? location.href.replace(/\/uniTS\/([^\/]+)\/.*$/, "/uniTS/$1") : "") + "/english/js/cuhk-survey.js?time=" + strDateTime;
423	
424	console.log("CUHK Survey starts here: " + strDateTime);
425	console.log("url2: " + strSurveyUrl);
426	
427	$.ajax({url:strSurveyUrl})
428		.done(function() { 
429			//console.log("cuhk-survey.js is loaded");
430		}).fail(function() {
431			console.log("cuhk-survey.js fails being loaded");
432		});	
433	
434	//20211126 - Set toggle QR code of WeChat CUHK Official Channels
435	  $("a.wechat_icon").click(function() {
436		  $(".wechat_qrcode", $(this).closest(".wechat_wrapper")).toggle();
437	  });
438	
439});
440
441//jquery extend function "setblockheight"
442$.fn.extend({
443  setBlockHeight: function() {
444
445		//Reset the height by removing the height attribute in style
446		this.css("height", "");
447		
448		var iMax = 0;
449		var iMin = 0;
450		var iBottomLayerMax = 0;
451		
452		this.each(function() {		
453			if ($(this).height() > iMax) {
454				iMax = $(this).height();
455			}
456			if ($(this).height() < iMin || iMin == 0) {
457				iMin = $(this).height();
458			}
459			
460			if ($(".bottom-layer", $(this)).length > 0 && $(".bottom-layer", $(this)).height() > iBottomLayerMax) {
461				iBottomLayerMax = $(".bottom-layer", $(this)).height();
462			}
463		});
464		
465		
466		//if (iMax != iMin) {
467			this.height(iMax + iBottomLayerMax);
468		//}
469		
470		return this;
471		
472  },
473  clearBlockHeight: function() {
474
475		//Reset the height by removing the height attribute in style
476		this.css("height", "");
477		return this;
478		
479  }
480})
481
482
483function createCookie(name,value,days) {
484  if (days) {
485    var date = new Date();
486    date.setTime(date.getTime()+(days*24*60*60*1000));
487    var expires = "; expires="+date.toGMTString();
488  }
489  else expires = "";
490  document.cookie = name+"="+value+expires+"; path=/";
491}
492
493function readCookie(name) {
494  var nameEQ = name + "=";
495  var ca = document.cookie.split(';');
496  for(var i=0;i < ca.length;i++) {
497    var c = ca[i];
498    while (c.charAt(0)==' ') c = c.substring(1,c.length);
499    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
500  }
501  return null;
502}
503
504

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.