Welcome to WebmasterWorld Guest from 174.129.151.95

Forum Moderators: open

Message Too Old, No Replies

Jquery and defined action for page name

   
3:26 pm on Dec 29, 2011 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



I have tested Jquery print page. There is function to print window like:

$('ul#printmyUL li.print a').click(function() {
window.print();
return false;
});


How to set correct page name to be printed inside window.print();

I like to detect current page name which will be printed with function window.print();

<?php
$searchURL = $_SERVER['REQUEST_URI'];
echo '<br />'.'The page URL with extension is: '.$searchURL;

$searchterm = $_SERVER['SCRIPT_FILENAME'];
echo '<br />'.'The folders and file extensions are: '.$searchterm;
//includes FOLDERS and FILE EXTENSIONS

function curPageName() {
return substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1);
}

echo '<br />'.'The current page name is: '.curPageName();
?>
5:49 pm on Dec 29, 2011 (gmt 0)

WebmasterWorld Senior Member drdoc is a WebmasterWorld Top Contributor of All Time 10+ Year Member



window.print()
will print the contents of the current browser window. The exact output is a combination of information inside the HTML of your document and your printer settings. In most cases, the
<title>
will be used as the name of the page (as listed on the printout).
5:58 pm on Dec 29, 2011 (gmt 0)

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Page name or file name?

You could do it like this. Since you're (properly) returning false from your function, you could pass a reference of the link itself. Using the anchor's href, you could get the file name:

<a href="page-one.html">Page One</a>


$('ul#printmyUL li.print a').click(function() {
return printWin(this.href);
});

function printWin(thelink) {
// alert('Link is ' + thelink);
window.print();
return false;
}

Or, if you want the anchor text,you can access the anchor's text content:

$('ul#printmyUL li.print a').click(function() {
return printWin($(this).text());
});

function printWin(anchor_text) {
// alert('Text is ' + anchor_text);
window.print();
return false;
}

Or, if you want "some other value" there's two ways off the top of my head. The first is in HTML5 you can use "data-anything" where HTML5 allows you to populate any attribute that begins with "data" onto any element:

<a href="page-one.html" data-pagetitle="This is my page title">Page One</a>

$('ul#printmyUL li.print a').click(function() {
return printWin($(this).attr('data-pagetitle'));
});

function printWin(anchor_text) {
// alert('Text is ' + anchor_text);
window.print();
return false;
}

For HTML4, or even HTML 5, you can use the title attribute or (ab)use the rel attribute:

<a href="page-one.html" title="This is my page title">Page One</a>

$('ul#printmyUL li.print a').click(function() {
return printWin($(this).attr('title'));
});

function printWin(title_attr) {
// alert('Title is ' + title_attr);
window.print();
return false;
}

<a href="page-one.html" rel="This is my page title">Page One</a>

$('ul#printmyUL li.print a').click(function() {
return printWin($(this).attr('rel'));
});

function printWin(mytitle) {
// alert('Rel value is ' + mytitle);
window.print();
return false;
}

There's probably many more ways. :-)