0

HEllo all : I must be a total noob - I have these two javascript scripts in the head of my html file but I want to hide the api number, and also the 2nd js. What do I need to write instead to have these js in another file that nobody can read when right clicking the mouse/show source code, and have the same google maps apearing in a div

I tried to put src="filename.js" but the google maps won't load at all.

Obfuscating the JS code ? But how can we put it after in a on the side file ? src="obfuscated code.js" won't load google maps at all Only putting obfiscated code in the head, but will it mess up with google indexing ?

  1.  <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=   
     ...&sensor=false"></script>
    
  2.     <script type="text/javascript">
        function initialize() {
            var latlng = new google.maps.LatLng(-19.991901,57.592607);
    
            var settings = {
                zoom: 1,
                disableDoubleClickZoom: true,
                draggable: false,
                scrollwheel: false,
                minZoom: 1,
                maxZoom: 18,
                center: latlng,
                mapTypeControl: false,
                navigationControl: true,
    navigationControlOptions: {style: google.maps.NavigationControlStyle.BIG},   
                mapTypeId: google.maps.MapTypeId.ROADMAP};
       var map = new google.maps.Map(document.getElementById("map_canvas"), settings);
    
    
    
            var companyImage = new google.maps.MarkerImage('images/logo.png',
                new google.maps.Size(100,50),
                new google.maps.Point(0,0),
                new google.maps.Point(50,50)
            );
    
                 var companyShadow = new google.maps.MarkerImage('images/logo_shadow.png',
                new google.maps.Size(130,50),
                new google.maps.Point(0,0),
                new google.maps.Point(65, 50));
    
            var companyPos = new google.maps.LatLng(-19.9913,57.592607);
    
            var companyMarker = new google.maps.Marker({
                position: companyPos,
                map: map,
                icon: companyImage,
                shadow: companyShadow,
                title:"Bur-Nas Beach",
                zIndex: 3});
    
    
    
    
            google.maps.event.addListener(companyMarker, 'click', function() {
                infowindow.open(map,companyMarker);
            });
        }
    </script>
    

2 Answers2

2

Ok there is a way you can do this by loading your javascript dynamicly ( that means injecting the script tag dynamicly trough some other javascript ) and removing it afterwards while it retains in browsers memory.

    (function() {

    var script_tag = document.createElement('script');
    script_tag.setAttribute("type","text/javascript");
    script_tag.setAttribute("src",
        "https://maps.googleapis.com/maps/api/js?key=YOURKEYHERE");
    if (script_tag.readyState) {
      script_tag.onreadystatechange = function () { // For old versions of IE
          if (this.readyState == 'complete' || this.readyState == 'loaded') {
              main();
          }
      };
    } else { // Other browsers
      script_tag.onload = main;
    }
    // Try to find the head, otherwise default to the documentElement
    (document.getElementsByTagName("head")[0] || document.documentElement).appendChild(script_tag);

    function removejscssfile(filename, filetype){
var targetelement=(filetype=="js")? "script" : (filetype=="css")? "link" : "none" //determine element type to create nodelist from
 var targetattr=(filetype=="js")? "src" : (filetype=="css")? "href" : "none" //determine corresponding attribute to test for
 var allsuspects=document.getElementsByTagName(targetelement)
 for (var i=allsuspects.length; i>=0; i--){ //search backwards within nodelist for matching elements to remove
  if (allsuspects[i] && allsuspects[i].getAttribute(targetattr)!=null && allsuspects[i].getAttribute(targetattr).indexOf(filename)!=-1)
   allsuspects[i].parentNode.removeChild(allsuspects[i]) //remove element by calling parentNode.removeChild()
 }
}



/******** Our main function called when Maps has loaded ********/
function main() {
//initiate maps here

//remove all occurences of your script on page
    removejscssfile("https://maps.googleapis.com/maps/api/js?key=YOURKEYHERE", "js") //remove all occurences of your script on page
    //Do some cool googlemaps based stuff here

}

})(); // We call our anonymous function immediately
JanCor
  • 605
  • 5
  • 3
0

One of the solution would be to obfuscate your file so it would not be human-readable. See this thread

Community
  • 1
  • 1
thébé
  • 114
  • 4
  • This really should have been a comment... In the future, please don't post link only answers - even if they are internal links. – Lix Apr 09 '13 at 17:06
  • Hi Lix, I would have love to do so but I don't yet have enough rep (50 Leave comments). I will keep that in mind when I have enough rep =) – thébé Apr 09 '13 at 17:08
  • IMO, one line of text including the link is not enough to be an answer with or with out 50 rep. FYI - the only reason I'm not downvoting is because I like your C&H avatar. You were saved this time... ;) – Lix Apr 09 '13 at 17:10
  • 1
    Good enough. I'll avoid it next time. *Just read your edit : Nice to see I could got away because you have good taste ;) – thébé Apr 09 '13 at 17:14