开发者

How can I check in JQuery if a page is displayed in print mode?

At first I would like to point out, that I am not a JQuery person, I am just beginning my work with it forced by the fact that our front-end guy is sick. please be delicate with me:)

In our application we switch to view mode by changing part of the url:

  • .../view/rest/.... means norm开发者_如何学编程al mode,
  • .../print/rest/... means print mode (site is stripped, different CSS applied).

I would like to check in javascript in which mode I currently am. We use JQuery in out project.

Please help!


Do a simple string search on window.location.pathname:

var isPrint = window.location.pathname.indexOf("/print/") > -1;
  • /view/rest
    alert(isPrint); // -> false
  • /print/rest
    alert(isPrint); // -> true

You can also perform a split and check a specific subfolder:

var folders = window.location.pathname.split("/");
var isPrint = folders[1] == "print";


You can examine the current URL by referencing location.href in your JavaScript code and determine it that way (no jQuery required).


You could use

var inPrint = (document.URL.indexOf("/print/") != -1);

(as long as there are no other /print/ folders)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜