...

View Full Version : Google Books JS problem



pluszone
09-26-2011, 01:49 PM
Hi All, I'm new to JS, and I'm trying to make google books previewed on my website. It works fine but if the book(ISBN) is not available for previewing, which only gives me nothing on the page :(

What I need is it give me a alert, or some echos,which tells something like "this book is not available for Previewing" when the ISBN is not found. Here is my code used on a php, and also a JS with the link. http://www.google.com/jsapi



<script type="text/javascript" src="js/jsapi.js"></script>
<script type="text/javascript">
google.load("books", "0", {"language" : "es-ES"});

function initialize() {
var viewer = new google.books.DefaultViewer(document.getElementById('viewerCanvas'));
viewer.load('ISBN:<?php echo $ISBN; ?>');
}
google.setOnLoadCallback(initialize);
</script>
</head>

<body>
<div id="viewerCanvas" style="width: 600px; height: 700px"></div>
</body>

pluszone
09-26-2011, 04:44 PM
help please.

xelawho
09-26-2011, 04:54 PM
what is the error that it throws if there is no ISBN? I imagine you could do something like this:



function initialize() {
if (!ISBN) {
//show some div saying "this book is not available for Previewing"
} else {
var viewer = new google.books.DefaultViewer(document.getElementById('viewerCanvas'));
viewer.load('ISBN:<?php echo $ISBN; ?>');
}
}

xelawho
09-26-2011, 05:36 PM
oops. I mean:



var ISBN="<?php echo $ISBN; ?>"

function initialize() {
if (!ISBN) {
//show some div saying "this book is not available for Previewing"
} else {
var viewer = new google.books.DefaultViewer(document.getElementById('viewerCanvas'));
viewer.load('ISBN:<?php echo $ISBN; ?>');
}
}


:D

pluszone
09-27-2011, 01:08 PM
thanks xelawho, I mean everybook already has a ISBN in my DB, but not all of them are available for Previewing through GOOGLE API, only some of them.When I browse these books which were not available, the window returns me nothing but a white page. I want to say something on that page instead of nothing. :>

xelawho
09-27-2011, 01:28 PM
right.

so when you try to open a book and google doesn't have it, does firebug show an error? do you by chance have a link to your page?

xelawho
09-27-2011, 02:42 PM
never mind - it's here (http://code.google.com/apis/books/docs/viewer/developers_guide.html#Handling_failed_initializations) in the documentation...



function alertNotFound() {
alert("could not embed the book!");
}

function initialize() {
var viewer = new google.books.DefaultViewer(document.getElementById('viewerCanvas'));
viewer.load('ISBN:<?php echo $ISBN; ?>', alertNotFound);
}


of course, you could swap the alert for whatever you wanted...

pluszone
09-27-2011, 03:07 PM
Thanks again. It works this time, and I just followed the "Handling failed initializations" of GOOGLE DEV Guide, everything is right there.
function alertNotFound() {
alert("could not embed the book!");
}
// plus
viewer.load('ISBN:<?php echo $id; ?>', alertNotFound);


Thanks!:D



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum