Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialDurdona Abdusamikovna
1,553 PointsI removed a function print(message) now my browser opens printer to print :)
Hello,
I was following a lecture and reviewing playlist.js it has a script below
var playList = [
'I Did It My Way',
'Respect',
'Imagine',
'Born to Run',
'Louie Louie',
'Maybellene'
];
function print(message) {
document.write(message);
}
if I remove function print (message) {.....} and run the code my browser opens printer ... I would like to know why ? Is it one of the JavaScript default functions ?
7 Answers
Marcus Parsons
15,719 PointsRight, so instead you should just use:
var playList = [
'I Did It My Way',
'Respect',
'Imagine',
'Born to Run',
'Louie Louie',
'Maybellene'
];
function printList(list) {
var listHTML = '<ol>';
for (var i = 0; i < list.length; i += 1){
listHTML += '<li>' + list[i] + '</li>';
}
listHTML += '</ol>';
//Changed from print(listHTML) to document.write(listHTML);
//Got rid of print() function because it's unnecessary
document.write(listHTML);
}
printList(playList);
Marcus Parsons
15,719 PointsHey Durdona!
What browser are you using to run this code?
Durdona Abdusamikovna
1,553 PointsGoogle Chrome
Durdona Abdusamikovna
1,553 PointsSorry forgot to say Hi :)
I don't see that function being related to the function printList () ... I linked playlist.js and my .js file to the HTML and don't know what causing it ... tried different things
Marcus Parsons
15,719 PointsThe print()
command is available in all browsers. I actually thought it was only Safari, but I learned something new. If you don't overwrite the print function, it will bring up a print dialogue for the current page.
In this case, you should just use document.write()
anyways whenever you are making print()
calls, as it is faster than creating a print function.
Durdona Abdusamikovna
1,553 PointsSo I was right it is one of the JavaScript functions cool :) Can you tell me why it opens print function when I remove it from the playlist.js ?
Marcus Parsons
15,719 PointsWhen you write in your own print function, it overwrites the browser's print function. So, when you don't have a custom print function, it uses the browser's default print function, which is to print the page.
But, you should just use document.write() instead of print() anyways.
Durdona Abdusamikovna
1,553 PointsLet me put the code so it will be easier to see
var playList = [
'I Did It My Way',
'Respect',
'Imagine',
'Born to Run',
'Louie Louie',
'Maybellene'
];
function print(message) {
document.write(message);
}
function printList(list) {
var listHTML = '<ol>';
for (var i = 0; i < list.length; i += 1){
listHTML += '<li>' + list[i] + '</li>';
}
listHTML += '</ol>';
print(listHTML);
}
printList(playList);
Durdona Abdusamikovna
1,553 Pointsinside braces I removed 'message'now I don't see my order list ...
Durdona Abdusamikovna
1,553 PointsOh I see you replaced
print(listHTML);
with
document.write(listHTML);
Cool I learned smth. again :) Yaaaaay :)
Durdona Abdusamikovna
1,553 PointsHey lifesaver :) Thank you again ! :)
Marcus Parsons
15,719 PointsYou're very welcome :P
Durdona Abdusamikovna
1,553 PointsDurdona Abdusamikovna
1,553 PointsYeah that's what I did and that's why I start seeing printer function...
I removed it because I don't see connection with actual code
Marcus Parsons
15,719 PointsMarcus Parsons
15,719 PointsNo, you won't see the print function this way. Carefully examine what I did. In fact, copy and paste my code into the console. There is no "print()" function being used at all here because it's completely unnecessary.
Marcus Parsons
15,719 PointsMarcus Parsons
15,719 PointsWhat your print function was doing was just making a 2nd function to pass in data to document.write() when you can skip all of that and just pass in data to document.write(). It's much faster to just say "document.write('Hi!')" than to make a function called print and say "print('Hi!')" which sends that to "document.write('Hi!')". See what I mean?