0

I am facing some problems while using drupal_add_js() along with drupal_goto().

I am using hook_nodeapi() to capture a node prepare condition; inside the "prepare" case, I am adding the following JavaScript code, which I need to close a Lightbox window):

drupal_add_js('$(document).ready(function() {
  window.parent.location = "'.$base_url.'";
  window.parent.Shadowbox.close();
})', 'inline');

The problem is that if I add a drupal_goto() before or after drupal_add_js(), then the JavaScript is not included in my page source.

I hope I am able to explain my problem. Can anyone please point me towards why this happens?

I know I can use JavaScript to implement drupal_goto() functionality, but in some cases the drupal_goto() comes from the drupal module itself and thus cannot be removed.

3

drupal_goto() alters the header of the page, then calls exit(). So if it's placed before the drupal_add_js(), the function will never be called, and if it's placed after, the header says "Go some place else," and you will be redirected as soon as possible, not waiting for jQuery to execute.

Why do you need to close the shadowbox in the first place if you are going to redirect the user somewhere else? It sounds like there's a fundamental problem to what you want to do.

  • i ll explain...i am using lightbox to have a node submit form. i want that when form is submitted the lightbox should be closed and the user to be redirected to a new page..Now to close the lightbox i need to call the above js code..so what do i do then to acheive the required.. – ayush May 31 '11 at 15:40
  • This doesn't explain why you need to close the lightbox. You are loading a new page, the lightbox is gonna die anyway. Alter #redirect on the form instead, and Drupal will handle the rest for you. api.drupal.org/api/drupal/… – Letharion Jun 4 '11 at 12:58
1

@Letharion's answer already explains why drupal_add_js() doesn't work together with drupal_goto().

I will add that an implementation of hook_nodeapi() is not supposed to redirect the user to a different page. If the user needs to be redirect to a different URL when it access a specific URL, then the menu callback associated with that URL needs to be replaced with another menu callback that verifies some conditions, and then redirects the user to the other URL; if the menu callback is one defined in another module, then you need to alter the menu callback associated with that URL using hook_menu_alter().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.