homepage Welcome to WebmasterWorld Guest from 54.197.7.135
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
Jquery and defined action for page name
toplisek




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

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();
?>

 

DrDoc




msg:4402222
 5:49 pm on Dec 29, 2011 (gmt 0)

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).
rocknbil




msg:4402228
 5:58 pm on Dec 29, 2011 (gmt 0)

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. :-)

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved