Beste,
ik kom er echt niet meer uit ik wil graag php in .js file gebruiken hoe kan ik dit doen?
Heeft hier iemand verstand van die mij hierbij uitleg kan bieden
Alvast bedankt,
Hieronder vindt u de code die ik tot nu toe hebt als ik hem in mijn .php bestand zet werkt het wel
maar ik wil het graag in een .js bestand hebben.
PHP Code:
/* Error messages for possible errors */
var error_address_empty = 'Please enter a valid address first.';
var error_invalid_address = 'This address is invalid. Make sure to enter your street number and city as well?';
var error_google_error = 'There was a problem processing your request, please try again.';
var error_no_map_info = 'Sorry! Map information is not available for this address.';
/**********************************************************************************************************************/
/* CHANGE THIS TO YOUR ADDRESS - The default address of your store, This address will display on the map on startup */
/**********************************************************************************************************************/
var default_address = '<?php echo $adres;?>, <?php echo $postcode;?>';
var current_address = null; /* Current address we are displaying, we save it here for directions */
var map = null; /* Instance of Google Maps object */
var geocoder = null; /* Instance of Google Deocoder object */
var gdir = null; /* Instance of Google Directions object */
var map_compatible = false; /* Whether or not user's browser is compatible to show the map */
/* Check if the browser is compatible */
if( GBrowserIsCompatible() ) {
map_compatible = true;
}
/* Initialize the map this will be called when the document is loaded from: <body onLoad="initialize_map();"> */
function initialize_map() {
if( map_compatible ) {
map = new GMap2(document.getElementById('map'));
geocoder = new GClientGeocoder();
show_address(default_address);
/* This displays the zoom controls for the map. If you don't want them just delete the line */
map.addControl(new GLargeMapControl());
/* This displays the map type. If you don't want that feature then just delete this */
map.addControl(new GMapTypeControl());
}
}
/* This function will move the map and shows the address passed to it */
function show_address(address) {
if( map_compatible && geocoder ) {
/* Save this address in current_address value to use later if user wants directions */
current_address = address;
geocoder.getLatLng(
address,
function( point ) {
if( !point ) {
alert(error_no_map_info);
} else {
map.setCenter(point, 15);
var marker = new GMarker(point);
map.addOverlay(marker);
marker.openInfoWindowHtml(address);
}
}
);
}
return false;
}
- php in javascript gebruiken
-
10-06-2011, 15:57 #1
- Berichten
- 154
- Lid sinds
- 15 Jaar
php in javascript gebruiken
-
In de schijnwerper
-
10-06-2011, 16:13 #2
- Berichten
- 587
- Lid sinds
- 16 Jaar
Re: php in javascript gebruiken
Je kan een .js bestand laten verwerken als een php bestand...
In je Htaccess:
<FilesMatch "\.(js)$">
AddHandler application/x-httpd-php .js
</FilesMatch>
-
10-06-2011, 16:26 #3
- Berichten
- 1.899
- Lid sinds
- 18 Jaar
Re: php in javascript gebruiken
In je .js file kun je geen php gebruiken, wat je kunt doen is in je PHP file een aantal variabelen vóór de HTML output laten instellen en daarna laten ophalen door je JS file. Andere optie is om data in een sessie/cookie op te slaan en deze op te halen met javascript.
-
10-06-2011, 16:34 #4
- Berichten
- 1.355
- Lid sinds
- 18 Jaar
Re: php in javascript gebruiken
Je maakt een php bestand aan, waarin je met php een javascript bestand opbouwt.
Door de pagina een header content-type mee te geven van javascript, lees de browser
het als een .js bestand.
Klikt misschien een beetje warrig, voor een complete uitleg zie de url hieronder;
http://www.javascriptkit.com/javatut...ernalphp.shtml
-
10-06-2011, 20:34 #5
- Berichten
- 390
- Lid sinds
- 15 Jaar
Re: php in javascript gebruiken
Op die manier is het inderdaad heel makkelijk en kan soms erg nuttig zijn. Zelf gebruik ik het bijvoorbeeld soms om alert boxes en andere tekst strings in de taal van de gebruiker te zetten.
En ik heb zelf zojuist geleerd dat ik dan beter ook de goeie content-type header kan zetten, oftewel ook namens mij bedankt Gijsbert!Laatst aangepast door Tjerk de Heer : 10-06-2011 om 20:39
Plaats een
- + Advertentie
- + Onderwerp
Marktplaats
Webmasterforum
- Websites algemeen
- Sitechecks
- Marketing
- Domeinen algemeen
- Waardebepaling
- CMS
- Wordpress
- Joomla
- Magento
- Google algemeen
- SEO
- Analytics
- Adsense
- Adwords
- HTML / XHTML
- CSS
- Programmeren
- PHP
- Javascript
- JQuery
- MySQL
- Ondernemen algemeen
- Belastingen
- Juridisch
- Grafisch ontwerp
- Hosting Algemeen
- Hardware Info
- Offtopic