2

Hi guys I have a a problem I think is a easy one but I don't find the problem. I have a "menu" where I want to send a data to another page when I click one button. So I write a function in js to open a new tab and the send the data. But when I do click, it is like if the function doesn't exist.

<div id="botones">
    &nbsp;&nbsp; 
    <input type="button" value="<?=L::misc_export?>" onclick="javascript:pasarDatosParaExportar();" name="boton" />
    &nbsp;&nbsp;
</div>

<script type="text/javascript">
function pasarDatosParaExportar() {
    window.open('calculos.exportar.php?fecha2='+'<?=$_POST['fecha2']?>'+'&hasta='<?=$_POST['hasta']?>'&cuartel='<?=(int)$_GET['cuartel']?>'','_blank');
}
</script>
2
  • 3
    The onclick attribute's value should be JavaScript code. Get rid of the javascript: there, that's not needed. Commented Dec 8, 2015 at 15:04
  • 1
    are you sure your string is getting populated correctly within window.open(... -- could be causing error. did you check the console? Commented Dec 8, 2015 at 15:08

3 Answers 3

1

Remove javascript: from content of your "onclick". It's for <a href="[HERE PUT javascript:]"

If you have link and want to put JS code into "href" parameter, should use it:

<a href="javascript:alert('success!')">click</a>

But you can do the same like this:

<a onclick="alert('success!')">click</a>
Sign up to request clarification or add additional context in comments.

2 Comments

While this is better practice, it does not prevent the event from firing, so this is not an answer. It would be better suited as a comment on the original question. jsfiddle.net/34a8wvuh
Better practice is putting it aside from HTML code. For example: document.getElementById('id').onclick = function() { /* body */ }. But you'r right of course - putting it in "href" wont fire link. But you can also put "return false;" in end of "onclick" attribute.
0

You should check the string delimiter characters (') in the parameter to the open function. I guess you probably mean to write something like this

function pasarDatosParaExportar() {
window.open('calculos.exportar.php?fecha2='+'<?=$_POST['fecha2']?>'+'&hasta=<?=$_POST['hasta']?>&cuartel=<?=(int)$_GET['cuartel']?>','_blank');
}

1 Comment

You're welcome. If my answer solved your problem perhaps you should mark it as "accepted answer".
0

Write the JavaScript code to define the function before its use in the div and above in code because html assumes that this code doesn't exists if it doesn't occur before its use. So just replace the div and script with each other. This should work fine. And there is no need to use javascript: before name of function. yo can use onclick="pasarDatosParaExportar();".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.