I am getting document.getElementById("pr_book") is null error

<script language="javascript">
google.load("books", "0");			 
                 function load_viewport() 
				 {
				    
			var viewportDiv = document.getElementById("viewport");
                    var identifier = '<?php echo $var ?>';
                    var viewer = new google.books.DefaultViewer(viewportDiv,
                    {showLinkChrome: false});
                    viewer.load(identifier,handle_not_found,success);
				    var d = document.getElementById('parent');
				    d.removeChild(viewportDiv);
				}
                 function handle_not_found() 
				 {
				    document.getElementById("pr_book").style.display = 'none';
					
				 }
				 function success()
				 {
				    document.getElementById("pr_book").style.display = 'inline';
				 }
				 google.setOnLoadCallback(function() {
				 load_viewport()
				 
				 });

function popitup(url) {
        newwindow=window.open(url,'book','height=650,width=850,left=50,top=50,location=no,status=no,scrollbars=no,resizable=no');
        if (window.focus) {newwindow.focus()}
        return false;
}

				
				
				
</script>			
<div id="parent"><div id="viewport" style="visibility:hidden;"></div></div>
 <?php  if ($Isbn !='') { ?>
<a id="pr_book"  style="display:none;" onclick="return popitup('<?php echo $this->getUrl('book/book/new',array('isbn'=>$Isbn,'name'=>$trimName))?>','book', 'width=850,height=650,left=50,top=50,location=no,status=no,scrollbars=no,resizable=no');" href="#" class="link-style_button1"><strong>PREVIEW THIS PRODUCT</strong>
<!--img src="<?php //echo $this->getSkinUrl($this->__('images/google_preview_btn.gif'))?>"></a-->
<?php }

first of all, fix line 41 so that you comment only the img tag, NOT that <a> tag.

then check the value of $Isbn. If it IS an empty string, then the if clause will not execute and your php code will not generate that link, which means that the browser will NOT get/see that link, leading you to the error you are seeing.

However if there are legitimate circumstances when that link will NOT be on the page, you need to first test the value returned by getElementById() :

...
var e =document.getElementById("pr_book");
if(e)
  e.style.display = '...';
...
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.