Chamada de java script função de diferentes tags
-
21-12-2019 - |
Pergunta
Eu tenho o seguinte código:
<script src="jquery-1.10.2.min.js"></script>
<script>
$('#year li').click(function() {
var text = $(this).text();
//alert('text is ' + text);
$.post("B.php", { text: text }, function(return_data, txtStatus, jqXHR) {
$('#result').html(return_data);
});
$.post("C.php", { text: text }, function(return_data, txtStatus, jqXHR) {
$('#subresult').html(return_data);
});
event.preventDefault() ;
});
</script>
Está escrito dentro da tag body do html.É possível chamar uma função js chamado initialize() que é escrito dentro da tag head da página html?se sim como?
Edições
Código para inicializar()
<script src="https://maps.googleapis.com/maps/api/js?sensor=false&libraries=visualization">
</script>
<script>
var map;
function initialize() {
var mapOptions = {
zoom: 5,
center: new google.maps.LatLng(2.8,-187.3),
mapTypeId: google.maps.MapTypeId.TERRAIN
};
map = new google.maps.Map(document.getElementById('map_canvas'),
mapOptions);
var script = document.createElement('script');
script.src = 'geojson';
document.getElementsByTagName('head')[0].appendChild(script);
}
window.eqfeed_callback = function(results) {
for (var i = 0; i < results.features.length; i++) {
var coords = results.features[i].geometry.coordinates;
var latLng = new google.maps.LatLng(coords[1],coords[0]);
var marker = new google.maps.Marker({
position: latLng,
map: map
});
}
}
</script>
geojson é um arquivo local que inicialmente contém nada, mas após o evento clicar contém geojson valor formatado de vários locais
Solução
É possível chamar uma função js chamado initialize() que é escrito dentro da tag head da página html?
Sim.
se sim como?
Supondo que é uma função global:
initialize();
Todo o script em uma página é colocado em um grande espaço de nomes.Scripts individuais não são isoladas uma da outra, a menos que você envolvê-los no escopo de funções.Assim:
<head>
<!-- ... -->
<script>
function initialize() {
alert("initialize!");
}
</script>
</head>
<body>
<script>
initialize();
</script>
<!-- ... -->
</body>
</html>
...funciona muito bem.(E não importa se os scripts são embutidas ou consulte externo .js
ficheiros.)