Skip to content

Instantly share code, notes, and snippets.

@jhauge
Created December 12, 2012 12:06
Show Gist options
  • Save jhauge/4267282 to your computer and use it in GitHub Desktop.
Save jhauge/4267282 to your computer and use it in GitHub Desktop.
Making current page properties available for javascript on all pages
<%@ Master Language="C#" MasterPageFile="~/umbraco/masterpages/default.master" AutoEventWireup="true"
CodeBehind="Master.master.cs" Inherits="Web.masterpages.Master" %>
<!--
This is the "root" master page that all other masterpages
in the website is based upon.
-->
<asp:Content runat="server" ContentPlaceHolderID="ContentPlaceHolderDefault"><!doctype html>
<html class="no-js" lang="en">
<head>
</head>
<body>
<!--
Script to load a variable telling about the current language
Insert before other scripts
-->
<script>
var UI = UI || {}; // Namespacing
UI.currentUmbNode = {
'pageId': '<%= CurrentNode.Id %>',
'name': '<%= CurrentNode.Name',
'nodeType': '<%= CurrentNode.NodeTypeAlias %>'
// Add more properties here if you need them
};
// If this Script block is included before other scriptblocks
// in the site, you will be able to access the current page
// properties using code like this:
UI.currentUmbNode.pageId;
</script>
</body>
</html>
</asp:Content>
using System;
using System.Web.UI;
using umbraco.NodeFactory;
namespace Web.masterpages
{
public partial class Master : MasterPage
{
public Master()
{
Init += PageInit;
}
public string CurrentNode { get; set; }
private void PageInit(object sender, EventArgs e)
{
// Make the current umbraco node available in the frontend .net code
CurrentNode = Node.GetCurrent();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment